1. 程式人生 > >2017ICPC瀋陽L——Tree【DFS】

2017ICPC瀋陽L——Tree【DFS】


#include<iostream>
#include<string.h>
#include<string>
#include<vector>
#include<set>
using namespace std;


const int maxn = 200000 + 10;
int vis[maxn];
vector<int>v[maxn];
int ans,n,k;


void dfs(int x, int pre)
{
vis[x] = 1;
for (int i = 0; i < v[x].size(); i++)
{
int temp = v[x][i];
if (temp == pre)continue;
dfs(temp, x);
vis[x] += vis[temp];
if (vis[temp] >= k&&n - vis[temp] >= k)ans++;
}
}


int main()
{
int T;
int a,b;
scanf("%d", &T);
while (T--)
{
memset(vis, 0, sizeof(vis));
ans = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
{
v[i].clear();
}

for (int i = 1; i < n; i++)
{
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, -1);
printf("%d\n", ans); 
}
return 0;
}