#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define N 200003
int cnt[N], n, k, ans;
vector<int> V[N];
void dfs(int u, int f) {
int i, v;
cnt[u] = 1;
for (i = 0;i < V[u].size();i++) {
v = V[u][i];
if (v == f) continue;
dfs(v, u);
cnt[u] += cnt[v];
if (cnt[v] >= k && n-cnt[v] >= k) ans++;
}
}
int main() {
int T, u, v, i;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
for (i = 1;i <= n;i++) V[i].clear();
for (i = 1;i < n;i++) {
scanf("%d%d", &u, &v);
V[u].push_back(v);
V[v].push_back(u);
}
ans = 0;
memset(cnt, 0, sizeof(cnt));
dfs(1, 0);
printf("%d\n", ans);
}
}
只要能将题目转化成找关键边即可。#include #include #include #include using namespace std;#define N 200003int cnt[N], n, k, ans;vector V[N];void dfs(int u, int f) { int i, v; cnt[u] = 1; for (i =