拓扑
CF 748 div3 E
[https://codeforc.es/problemset/problem/1593/E]
拓扑思想
一开始想复杂了
还有,arr->clear(); 并不好用
#include<bits/stdc++.h>
//#include<gl/glut.h>
//#include<ctime>
#define ll long long
#define mes(a,b) memset(a,b,sizeof(a))
#define ctn continue
#define ull unsigned long long
#pragma warning(disable:4996)
#define tgg cout<<"---------------"<<endl;
const ll linf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
using namespace std;
const double pi = acos(-1);
const ll maxn = 4e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const ull p = 131;
const ull pc = 13331;
inline ll gcd(int a, int b) {
while (b ^= a ^= b ^= a %= b);
return a;
}
vector<int> arr[maxn];
queue<int> q;
int val[maxn];
int deg[maxn];
void dfs() {
while (!q.empty()) {
int now = q.front(); q.pop();
for (int i = 0; i < arr[now].size(); i++) {
int nxt = arr[now][i];
if (deg[nxt] <= 1) ctn;
val[nxt] = val[now] + 1;
deg[nxt] --;
if (deg[nxt] <= 1) q.push(nxt);
}
}
}
int main(int argc, char* argv[]) {
int t;
cin >> t;
while(t--) {
int n, k;
cin >> n >> k;
while (!q.empty()) {
q.pop();
}
getchar();
mes(deg, 0);
for (int i = 1; i <= n; i++) arr[i].clear();
for (int i = 1; i <= n - 1; i++) {
int l, r;
cin >> l >> r;
arr[l].push_back(r);
arr[r].push_back(l);
deg[l]++;
deg[r]++;
}
for (int i = 1; i <= n; i++) {
if (arr[i].size() <= 1) {
val[i] = 1;
q.push(i);
}
}
dfs();
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (val[i] <= k) ans++;
}
cout << n - ans << endl;
}
return 0;
}