ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

E. Gardener and Tree 拓扑排序板子

2022-07-30 20:36:29  阅读:162  来源: 互联网

标签:typedef const int Tree long 板子 include Gardener define


E. Gardener and Tree

将度为1的顶点加入队列,做拓扑排序,记录点的遍历层次,答案为 n-前k层的点

 

//  AC one more times

////////////////////////////////////////INCLUDE//////////////////////////////////////////

#include <iostream>
#include <algorithm>
#include <cstdio>
#include<string>
#include<string.h>
#include <cstring>
#include <complex>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <list>
#include <bitset>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <random>
#include <iterator>
#include <time.h>

using namespace std;
/////////////////////////////////////////DEFINE//////////////////////////////////////////

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define inf64 0x3f3f3f3f3f3f3f3f

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long,long long> PLL;

////////////////////////////////////////CONST////////////////////////////////////////////

const int  inf = 0x3f3f3f3f;
const int maxn = 2e6 + 6;
const double eps = 1e-8;
const int mod = 1000000;

///////////////////////////////////////FUNCTION//////////////////////////////////////////

template<typename T>
void init(T q[], int n, T val){   for (int i = 0; i <= n; i++) q[i] = val;  }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool cmp(int c, int d) { return c > d; }



/*
2 200000
1 2
*/
void solve()
{

    int n;  cin>>n;
    int k;  cin>>k;
    int st[n+10];memset(st,0,sizeof st);
    vector<int> son[n+10];
    int d[n+10];memset(d,0,sizeof d);
    for(int i=1;i<=n+3;i++)
        son[i].clear();
    
    for(int i=1;i<=n-1;i++)
    {
        int u,v;    cin>>u>>v;
        son[u].pb(v);
        son[v].pb(u);
        d[u]++,d[v]++;
    }
    queue<int> q;
    for(int i=1;i<=n;i++)
    {
        if(d[i]==1)
            q.push(i),st[i]=1;
    }

    while(!q.empty())
    {
        int t=q.front();    q.pop();
        for(auto &it:son[t])
        {
            int j=it;
            if(--d[j]==1)
            {
                st[j]=st[t]+1;
                q.push(j);
            }
        }
    }   
    int ans=0;
    for(int i=1;i<=n;i++)
        if(st[i]<=k)   ans++;
    cout<<n-ans<<endl;

    return;
}    
   
int main()
{
    std::ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    
    int T;cin>>T;while(T--)
        solve();    


    return 0;
}
/*
 *  ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
 *  │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
 *  └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
 *  ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
 *  │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
 *  ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
 *  │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
 *  ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
 *  │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
 *  ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
 *  │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
 *  ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
 *  │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
 *  └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
 */

 

 

标签:typedef,const,int,Tree,long,板子,include,Gardener,define
来源: https://www.cnblogs.com/magicat/p/16535716.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有