鱼C论坛

 找回密码
 立即注册
查看: 2146|回复: 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 | 显示全部楼层
#include<cstdio>
#define N 10000001

using namespace std;

int tr[N],num[N];
int findroot(int x)
{
        if(tr[x]==-1)
                return x;
        else 
        {
                int tmp=findroot(tr[x]);
                tr[x]=tmp;
                return tmp;
        }
}
int main()
{
        int n;
        while(scanf("%d",&n)!=EOF)
        {
                for(int i=0;i<=N;i++)
                {
                        tr[i]=-1;
                        num[i]=1;
                }
                int i=n;
                while(i--)
                {
                        int a,b;
                        scanf("%d%d",&a,&b);
                        a=findroot(a);
                        b=findroot(b);
                        if(a!=b)
                        {
                                tr[b]=a;
                                num[a]+=num[b];
                        }
                }
                int ans=1;
                for(i=1;i<=n;i++)
                        if(tr[i]==-1&&num[i]>ans)
                                ans=num[i];
                printf("%d\n",ans);
        }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-6 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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