HDU-1856 More is better

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856

题意:给n个朋友关系,所有间接或直接的朋友算一组,求人数最多的组的人数

思路:裸的并查集找最大连通块,但是WA了6发才A了,最后改成1遍历到1e7找最大值

代码:

 1 //#include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<vector>
 4 #include<stack>
 5 #include<string>
 6 #include<cstdio>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<map>
10 #include<set>
11 #include<cmath>
12 #include<iomanip>
13 #define inf 0x7f7f7f7f
14 //#define scanf scanf_s
15 using namespace std;
16 typedef long long ll;
17 const ll M = ll(1e7) + 5;
18 struct node {
19     int fa, cnt;
20 };
21 node root[M];
22 int maxn;
23 void init() {
24     for (int i = 0; i <= M; i++) {
25         root[i].fa = i;
26         root[i].cnt = 1;
27     }
28 }
29 int find(int x) {
30     if(x!=root[x].fa)
31         root[x].fa=find(root[x].fa);
32     return root[x].fa;
33 }
34 void merge(int x, int y) {
35     x = find(x);
36     y = find(y);
37     if(x!=y){
38         root[x].fa=y;
39         root[y].cnt+=root[x].cnt;
40     }
41 }
42 int maxnum;
43 signed main()
44 {
45     int t; 
46     while (~scanf("%d",&t)) {
47         init();
48         maxn = -1;
49         for(int i = 0; i < t; i++) {
50             int x, y;
51             scanf("%d%d", &x, &y);
52             merge(x, y);
53             maxnum=max(maxnum,max(x,y));
54             //output(t);
55         }
56         for(int i=1;i<=M;i++)
57             if(root[i].fa==i){
58                 maxn=max(maxn,root[i].cnt);
59             }
60         printf("%d\n", maxn);
61     }
62     return 0;
63 }

猜你喜欢

转载自www.cnblogs.com/harutomimori/p/10884108.html