鱼C论坛

 找回密码
 立即注册
查看: 47|回复: 1

[学习笔记] 024:More is better

[复制链接]
最佳答案
17 
发表于 2018-2-13 11:09:05 | 显示全部楼层 |阅读模式

马上注册加入鱼C,享用更多服务吧^_^

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 Messj 于 2018-6-8 23:40 编辑

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.

本帖被以下淘专辑推荐:

最佳答案
17 
 楼主| 发表于 2018-2-13 11:09:38 | 显示全部楼层
  1. #include<cstdio>
  2. #define N 10000001

  3. using namespace std;

  4. int tr[N],num[N];
  5. int findroot(int x)
  6. {
  7.         if(tr[x]==-1)
  8.                 return x;
  9.         else
  10.         {
  11.                 int tmp=findroot(tr[x]);
  12.                 tr[x]=tmp;
  13.                 return tmp;
  14.         }
  15. }
  16. int main()
  17. {
  18.         int n;
  19.         while(scanf("%d",&n)!=EOF)
  20.         {
  21.                 for(int i=0;i<=N;i++)
  22.                 {
  23.                         tr[i]=-1;
  24.                         num[i]=1;
  25.                 }
  26.                 int i=n;
  27.                 while(i--)
  28.                 {
  29.                         int a,b;
  30.                         scanf("%d%d",&a,&b);
  31.                         a=findroot(a);
  32.                         b=findroot(b);
  33.                         if(a!=b)
  34.                         {
  35.                                 tr[b]=a;
  36.                                 num[a]+=num[b];
  37.                         }
  38.                 }
  39.                 int ans=1;
  40.                 for(i=1;i<=n;i++)
  41.                         if(tr[i]==-1&&num[i]>ans)
  42.                                 ans=num[i];
  43.                 printf("%d\n",ans);
  44.         }
  45.         return 0;
  46. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

小甲鱼强烈推荐上一条 /1 下一条

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号

GMT+8, 2018-8-15 09:45

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表