首页 技术 正文
技术 2022年11月12日
0 收藏 924 点赞 3,235 浏览 1375 个字

题目描述

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.

输入

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)

输出

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入

3 1 3 1 5 2 5 4 3 2 3 4 1 6 2 6

样例输出

4 5 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;int f[];
int num[];int findFather(int x)
{
int a=x;
while(x!=f[x])
{
x=f[x];
}
while(a!=f[a])
{
int z=a;
a=f[a];
f[z]=x;
}
return x;
}
int ans=;
void unionf(int a,int b)
{
int fa=findFather(a);
int fb=findFather(b);
if(fa!=fb)
{
f[fa]=fb;
num[fb]=num[fa]+num[fb];
if(num[fb]>ans)
{
ans=num[fb];
}
}
}void init()
{
for(int i=;i<;i++)
{
f[i]=i;
num[i]=;
}
}int main()
{
int n;
while(cin>>n)
{
init();
int a,b;
ans=;
for(int i=;i<n;i++)
{
cin>>a>>b;
unionf(a,b);
//cout<<"ans:"<<ans<<endl;
}
cout<<ans<<endl;
}
return ;
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,495
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,741
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298