HDU 4707

这个题目是一个巧用并查集的经典题目。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707

           Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 652    Accepted Submission(s): 334


Problem Description
One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three days. Nothing but cockroaches was caught. He got the map of the school and foundthat there is no cyclic path and every location in the school can be reached from his room. The trap’s manual mention that the pet will always come back if it still in somewhere nearer than distance D. Your task is to help Lin Ji to find out how many possible locations the hamster may found given the map of the school. Assume that the hamster is still hiding in somewhere in the school and distance between each adjacent locations is always one distance unit.
 

 

Input
The input contains multiple test cases. Thefirst line is a positive integer T (0<T<=10), the number of test cases. For each test cases, the first line has two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0.
 

 

Output
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
 

 

Sample Input
1 10 2 0 1 0 2 0 3 1 4 1 5 2 6 3 7 4 8 6 9
 

 

Sample Output
2
 

 

Source
 

 

Recommend
liuyiding
 

 

题意就是N个点是一个连通图,求图中与0点的距离大于D的点的个数。

显然以0点为root,我们只要知道每一个点的深度就可以了。

所以,我们可以以0点为root建立并查集,每个节点有两个属性,一是父节点f,二是深度d,代码里分别用连个数组储存。

注意,findset()中,节点深度更新是有条件的,即其父节点不能为根节点,详见代码。

上代码:

 1 #include<stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 #include<queue>
 5 #include<math.h>
 6 #include<algorithm>
 7 #include<string.h>
 8 
 9 #define repA(p,q,i)  for( int (i)=(p); (i)!=(q); ++(i) )
10 #define repAE(p,q,i)  for( int (i)=(p); (i)<=(q); ++(i) )
11 #define repD(p,q,i)  for( int (i)=(p); (i)!=(q); --(i) )
12 #define repDE(p,q,i)  for( int (i)=(p); (i)>=(q); --(i) )
13 #define range 100010
14 
15 int D[range];
16 int f[range];
17 int N,deep;
18 
19 int findset( int i );
20 
21 int main()
22 {
23     int test;  scanf("%d",&test);
24     while(test--)
25     {
26         scanf("%d%d",&N,&deep);
27         repAE(0,N,i)
28         {  f[i]=i;  D[i]=0;  }
29         int a,b,p,q;
30         repA(1,N,i)
31         {
32             scanf("%d%d",&a,&b);
33             p=findset(a) ;  q=findset(b) ;
34             //printf("p,q: %d %d\n",p,q);
35             if( p == q && abs( D[a] - D[b] ) > 1 )
36             {
37                  if(D[a] < D[b]) 
38                     {  D[a]=D[b]+1;  }
39                  else  { D[b]=D[a]+1;  }
40             }
41             else
42             { f[q]=a; D[q]=D[a]+1; }
43         }
44         repA(0,N,i)
45           findset(i);
46         //printf("%d %d %d\n",D[1],D[2],D[3]);
47         //printf("%d %d %d %d\n",D[4],D[5],D[6],D[7]);
48         //printf("%d %d\n",D[8],D[9]);
49         int all=0;
50         repA(0,N,i)
51           if( abs(D[i]-D[0]) > deep )
52             ++all;
53         printf("%d\n",all);
54     }
55 }
56 
57 int findset( int i )
58 {
59     int temp;
60     if( f[i] != i )
61     {
62         temp = findset( f[i] ) ;
63         if( f[ f[i] ] != f[i] ) D[i] = D[ f[i] ] + 1 ;
64         f[i]=temp;
65     }
66     return  f[i] ;
67 }
View Code

 

posted on 2013-09-13 11:24  码农之上~  阅读(324)  评论(0编辑  收藏  举报

导航