HDOJ---1856 More is better[并查集求节点最多的树的节点数量]

More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 6438    Accepted Submission(s): 2383


Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
 

 

Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
 

 

Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
 

 

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

 

Sample Output
4 2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
 

 

Author
lxlcrystal@TJU
 

 

Source
 

 

Recommend
lcy
 
 
 
 
 
 
 
用一个变量存最大值
code:
 1 #include <iostream>   

 2 #include <iomanip>   

 3 #include <fstream>   

 4 #include <sstream>   

 5 #include <algorithm>   

 6 #include <string>   

 7 #include <set>   

 8 #include <utility>   

 9 #include <queue>   

10 #include <stack>   

11 #include <list>   

12 #include <vector>   

13 #include <cstdio>   

14 #include <cstdlib>   

15 #include <cstring>   

16 #include <cmath>   

17 #include <ctime>   

18 #include <ctype.h> 

19 using namespace std;

20 

21 #define MAXN 10000010

22 

23 int father[MAXN];

24 int cnt[MAXN];

25 int maxnum;

26 

27 void init()

28 {

29     for(int i=0;i<MAXN;i++)

30     {

31         father[i]=i;

32         cnt[i]=1;

33     }

34 }

35 

36 int findset(int v)

37 {

38     int t1,t2=v;

39     while(v!=father[v])

40         v=father[v];

41     while(t2!=father[t2])

42     {

43         t1=father[t2];

44         father[t2]=v;

45         t2=t1;

46     }

47     return v;

48 }

49 

50 void Union(int a,int b)

51 {

52     a=findset(a);

53     b=findset(b);

54     if(a!=b)

55     {

56         father[a]=b;

57         cnt[b]+=cnt[a];

58         maxnum=maxnum>cnt[b]?maxnum:cnt[b];

59     }

60 }

61 

62 int main()

63 {

64     int n;

65     int a,b;

66     while(~scanf("%d",&n))

67     {

68         maxnum=1;

69         init();

70         while(n--)

71         {

72             scanf("%d%d",&a,&b);

73             Union(a,b);

74         }

75         printf("%d\n",maxnum);

76     }

77     return 0;

78 }

 

你可能感兴趣的:(more)