# Tree

Problem Description
Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem.
Now we decide to colour its nodes with k distinct colours, labelled from 1 to k. Then for each colour i = 1, 2, · · · , k, define Ei as the minimum subset of edges connecting all nodes coloured by i. If there is no node of the tree coloured by a specified colour i, Ei will be empty.
Try to decide a colour scheme to maximize the size of E1 ∩ E2 · · · ∩ Ek, and output its size.

Input
The first line of input contains an integer T (1 ≤ T ≤ 1000), indicating the total number of test cases.
For each case, the first line contains two positive integers n which is the size of the tree and k (k ≤ 500) which is the number of colours. Each of the following n - 1 lines contains two integers x and y describing an edge between them. We are sure that the given graph is a tree.
The summation of n in input is smaller than or equal to 200000.

Output
For each test case, output the maximum size of E1 ∩ E1 ... ∩ Ek.

Sample Input
3 4 2 1 2 2 3 3 4 4 2 1 2 1 3 1 4 6 3 1 2 2 3 3 4 3 5 6 2

Sample Output
1 0 1

#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long int ll;
const int MAXV=200005;

int k,ans;
struct edge{
int v1,v2,next;
}e[MAXV];

int n,edge_num;
int num[MAXV];//记录某个点的子树的大小

void insert_edge(int v1,int v2){
e[edge_num].v1=v1;
e[edge_num].v2=v2;

e[edge_num].v1=v2;
e[edge_num].v2=v1;
}

void dfs(int v,int fa){
num[v]=1;
if(e[i].v2!=fa){
dfs(e[i].v2,v);
num[v]+=num[e[i].v2];

//每条边的右边子树与左边子树的大小，左边=n-右边
if(num[e[i].v2]>=k&&n-num[e[i].v2]>=k)
ans++;
}
}

}

int main(){
int t;
scanf("%d",&t);

while(t--){
edge_num=0;
memset(num,0,sizeof(num));

scanf("%d%d",&n,&k);
int a,b;
for(int i=0;i<n-1;i++){
scanf("%d%d",&a,&b);
insert_edge(a,b);
}
ans=0;
dfs(1,0);
printf("%d\n",ans);

}

return 0;
}