题目描述

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 ;
}
 

最新文章

  1. Curling 2.0
  2. 创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。
  3. Regex
  4. LeetCode:Unique Binary Search Trees I II
  5. 非空验证(源代码Java版)
  6. ReactNative
  7. 爬虫工具fiddle在firefox浏览器中的使用
  8. Java 持久化之 --io流与序列化操作
  9. 好的UI管理后台
  10. RSA 前段加密 java 后台解密 已调试通过
  11. SpringBoot之RabbitMQ的使用
  12. mac修改本机mysql的root密码
  13. centos6.6安装hadoop-2.5.0(四、hadoop HA安装)
  14. 使用dynamic和MEF实现轻量级的AOP组件 ---- 系列文章
  15. g++编译后中文显示乱码解决方案(c++)
  16. ffplay源码分析5-图像格式转换
  17. Linux下安装matlab2014a
  18. 028.Zabbix常见故障
  19. OpenHaptics编程环境搭建
  20. 自动化部署必备技能—搭建YUM仓库

热门文章

  1. Delphi Compiler Bug?
  2. CentOS6安装各种大数据软件 第三章:Linux基础软件的安装
  3. My First
  4. Ajax的跨域请求——JSONP的使用
  5. 20155203 《信息安全技术》 实验2 Windows口令破解
  6. 微信小程序点击按钮,修改状态
  7. js的视频和音频采集
  8. idea alt+enter导包时被锁定导某一个包时的解决方法
  9. 【JUC源码解析】ReentrantReadWriteLock
  10. 【MySQL函数】MySQL 5.5从零开始学第六章