前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ----More is better(并查集)

HDUOJ----More is better(并查集)

作者头像
Gxjun
发布2018-03-21 13:00:10
7350
发布2018-03-21 13:00:10
举报
文章被收录于专栏:mlmlml

More is better

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

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

HDU 2007 Programming Contest - Final

Recommend

lcy

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

并查集可以解。。。。求的是集合里面元素的个数:即求出最大的秩就行了!

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #define maxn 100001
 4 using namespace std;
 5 int father[maxn+5],rank[maxn+5];
 6 void inite(int n)
 7 {
 8     for(int i=1;i<=n;i++)
 9     {
10         father[i]=i;
11         rank[i]=1;
12     }
13 }
14 
15 int findset(int x)
16 {
17    if(x!=father[x])
18    {
19        father[x]=findset(father[x]);
20    }
21   return father[x];
22 }
23 
24 void unset(int x,int y)
25 {
26     x=findset(x);
27     y=findset(y);
28     if(x==y) return ;
29    if(rank[x]>rank[y])
30    {
31        father[y]=father[x];
32        rank[x]+=rank[y];
33    }
34    else
35    {
36        father[x]=father[y];
37        rank[y]+=rank[x];
38    }
39 }
40 
41 int main()
42 {
43     int n,a,b,i,max;
44     while(scanf("%d",&n)!=EOF)
45     {
46         inite(maxn);
47         for(i=0;i<n;i++)
48         {
49             scanf("%d %d",&a,&b);
50             unset(a,b);
51         }
52      for(i=1;i<=maxn;i++)
53      {
54         if(i==1||max<rank[i])
55          max=rank[i];
56      }
57      printf("%d\n",max);
58     }
59   return 0;
60 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • More is better
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档