Gardener and Tree(1600 拓扑排序)
1 /**\ 2 https://codeforces.com/problemset/problem/1593/E 3 拓扑排序 每次去掉k次叶子节点, 问最后能留下多少个顶点 4 (本题以入度为1作为叶子节点) 5 \**/ 6 #include <bits/stdc++.h > 7 using namespace std; 8 #define fi first 9 #define se second 10 11 #define go continue 12 #define int long long 13 #define PII pair<int, int> 14 #define ytz int _; cin >> _; while(_--) 15 16 #define fory(i,a,b) for(int i = a; i <= b; ++i) 17 #define debug(a) cout << #a << " = " << a <<endl; 18 #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 19 20 const int N = 4e5 + 10; 21 int n, k; 22 23 int vis[N], inc[N]; 24 vector<int> g[N]; 25 signed main() 26 { 27 IOS; 28 ytz 29 { 30 memset(vis, 0, sizeof(int)*(n + 10)); 31 memset(inc, 0, sizeof(int)*(n + 10)); 32 fory(i, 1, n) g[i].clear(); 33 cin >> n >> k; 34 fory(i, 1, n - 1) 35 { 36 int u, v; 37 cin >> u >> v; 38 g[v].push_back(u); 39 g[u].push_back(v); 40 inc[v]++; 41 inc[u]++; 42 } 43 if(n == 1) 44 { 45 if(k >= 1) cout << "0\n"; 46 else cout << "1\n"; 47 go; 48 } 49 queue<PII> q; 50 fory(i, 1, n) 51 { 52 if(inc[i] == 1) 53 { 54 q.push({i, 1}); 55 vis[i] = 1; 56 } 57 } 58 59 while(!q.empty()) 60 { 61 62 auto now = q.front(); 63 if(now.se > k) break; 64 int x = now.fi; 65 vis[x] = 1; 66 q.pop(); 67 for(auto c : g[x]) 68 { 69 inc[c]--; 70 if(inc[c] == 1 && now.se + 1 <= k) 71 { 72 q.push({c, now.se + 1}); 73 } 74 } 75 } 76 int ok = 0; 77 fory(i, 1, n) 78 { 79 if(!vis[i]) ok++; 80 } 81 cout << ok << "\n"; 82 } 83 84 return 0; 85 }