HDU 1856 More is better(并查集)

题目:

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.

InputThe 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)OutputThe 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.

大意:

王先生想让一些男孩帮他做一个项目。因为这个项目相当复杂,男孩越多,情况就越好。当然也有一定的要求。

王先生选了一个大得足以容纳男孩的房间。未被选中的男孩必须立即离开房间。房间里有10000000个男孩,从一开始就从1到10000000。

在王先生的选择之后,他们中还有两个人仍然是朋友(直接或间接),或者只剩下一个男孩。给定所有的直接朋友对,你应该决定最好的方式

分析:

假设A和B是朋友,B和C是朋友,那么A和C也是朋友(间接),以此类推,找出最大的朋友数,并查集加一个数组标记;
附代码;

 1 #include<map>
 2 #include<cmath>
 3 #include<queue>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 int p[1000005],r[1000005];
11 int find(int x)
12 {
13     int r=x;
14     while (p[r]!=r)
15         r=p[r ];
16     int j,i=x;
17     while(i!=r)
18     {
19         j=p[i ];
20         p[i ]=r;
21         i=j;
22     }
23     return r;
24 }
25 int main()
26 {
27     int n;
28     while(~scanf("%d",&n))
29     {
30         if(n==0)
31         {
32             printf("1\n");
33             continue;
34         }
35         for(int i=1; i<1000005; i++)
36         {
37             p[i]=i;
38             r[i]=1;
39         }
40         int all=0;
41         for(int i=0; i<n; i++)
42         {
43             int f1,f2,p1,p2;
44             scanf("%d%d",&p1,&p2);
45             f1=find(p1);
46             f2=find(p2);
47             if(f1!=f2)
48             {
49                 p[f1]=f2;
50                 r[f2]+=r[f1];
51             }
52             if(all<r[p[p2]])
53                 all=r[p[p2]];
54         }
55         printf("%d\n",all);
56     }
57     return 0;
58 }

第一次写博客,请多指教!!!

转载于:https://www.cnblogs.com/She-Chuan/p/9491766.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值