鱼C论坛

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

[技术交流] 024:More is better

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

马上注册,结交更多好友,享用更多功能^_^

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

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.

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 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. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 17:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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