[HDU 1856] More is better

并查集解决最大朋友圈问题
本文探讨如何使用并查集解决求解最大朋友圈数量的问题,包括算法优化、特判处理及实例分析。

More is better

Time Limit: 5000/1000 MS (Java/Others)    

Memory Limit: 327680/102400 K (Java/Others)

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.
 
【题解】并查集问题:求最大rank
我加入了路径压缩和按秩合并。
然后轻松AC了= =
注意的是有一个特判,n==0时,输出1.
注意不需要开始就全部初始化,那样复杂度为O(10000005*T) T为数据组数,那么肯定受不了。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int pre[10000005],r[10000005],n,maxx;
 4 bool vis[10000005];
 5 int find(int x) {
 6     int r=x;
 7     while(pre[r]!=r) r=pre[r];
 8     int i=x,j;
 9     while(i!=r) {
10         j=pre[i];
11         pre[i]=r;
12         i=j;
13     }
14     return r;
15 }
16 void join(int a,int b) {
17     int a1=find(a),b1=find(b);
18     if (a1!=b1) {
19         pre[a1]=b1;
20         r[b1]+=r[a1];
21         if (r[b1]>maxx) maxx=r[b1];
22     }
23 }
24 
25 int main() {
26     while(scanf("%d",&n)!=EOF) {
27         maxx=-1;memset(vis,0,sizeof(vis));
28         if(n==0) {printf("1\n"); continue;}
29         for (int i=1;i<=n;++i) {
30             int a,b;
31             scanf("%d%d",&a,&b);
32             if(!vis[a]) {
33                 vis[a]=1;
34                 pre[a]=a;
35                 r[a]=1;
36             }
37             if(!vis[b]) {
38                 vis[b]=1;
39                 pre[b]=b;
40                 r[b]=1;
41             }
42             join(a,b);
43         }
44         printf("%d\n",maxx);
45     }
46     return 0;
47 }
View Code

 

 

转载于:https://www.cnblogs.com/TonyNeal/p/hdu1856.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值