标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1809 Accepted Submission(s): 874
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define maxn 100100 5 using namespace std; 6 int p[maxn],a,b; 7 void init() 8 { 9 int i; 10 for(i=0;i<a;i++) 11 p[i]=i; 12 } 13 int find(int x) 14 { 15 int t=x; 16 while(t!=p[t])//不用压缩路径 17 t=p[t]; 18 return t; 19 } 20 int fun(int x) 21 { 22 int i=x,num=0; 23 while(i!=p[i]) 24 { 25 //p[i]=find(i);//查找x的深度 26 i=p[i]; 27 num++; 28 } 29 return num; 30 } 31 32 int main() 33 { 34 int N,n,i,x,y,cot; 35 scanf("%d",&N); 36 while(N--) 37 { 38 39 cot=0; 40 scanf("%d%d",&a,&b); 41 init(); 42 for(i=0;i<a-1;i++) 43 { 44 scanf("%d%d",&x,&y); 45 p[y]=x; 46 } 47 for(i=a-1;i>=0;i--) 48 { 49 if(find(i)==0) 50 if(fun(i)>b) 51 cot++; 52 } 53 printf("%d\n",cot); 54 } 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4712134.html