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.

 
  题意:
    要找一些人完成一项工程。要求最后挑选出的人之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个人(最少挑一个)。
    在基础的并查集上加个数组记录集合的数量。
 #include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define N 100 int Tree[N];
int findRoot(int x){//查找x的根节点
if(Tree[x]==-)
return x;
else{
int temp=findRoot(Tree[x]);
Tree[x]=temp;
return temp;
}
} int main()
{
int sum[N];//结点i为根的树的节点数
int n;
scanf("%d",&n);
//初始化
for(int i=;i<N;i++){
Tree[i]=-;
sum[i]=;
} while(n--){
int a,b;
scanf("%d %d",&a,&b);
a=findRoot(a);//找到a所在树的根节点
b=findRoot(b);//找到b所在树的根节点
if(a!=b){//ab不在一棵树上
Tree[a]=b;//a树连接到b树上
sum[b]+=sum[a];//以b为根节点的树上节点数添加a上节点的数目
}
}
int ans=;
for(int i=;i<N;i++){
if(Tree[i]==- && sum[i]>ans)//找到最大值
ans=sum[i];
}
printf("%d",ans);
return ;
}

这里特别说明一下,王道上面的答案有问题,问题出在第42行,因为N=100,而 i 作为数组的下标,应该是0-99,100越界了,如果按照王道上面的做法,最后答案会成100.

最新文章

  1. APP测试入门篇之APP基础知识(001)
  2. Yeoman自动构建js项目
  3. 003--VS2013 C++ 多边形绘制
  4. hibernate一个注册小例子
  5. SDUT1466双向队列
  6. redis的使用
  7. easyui使用时出现这个Uncaught TypeError: Cannot read property &#39;combo&#39; of undefined
  8. C# 程序关闭和进程关闭
  9. linux 入侵检查转载
  10. 算法优化(动态规划):COGS 2009. [USACO Mar09]餐厅清扫
  11. 为github帐号添加SSH keys
  12. VS2010调用VLFeat
  13. 边学边做,简单的 GraphQL 实例
  14. Async/Await是这样简化JavaScript代码的
  15. 百度前端学院-基础学院-第七天到第八天之BFC
  16. HDU5758 Explorer Bo 思维+树形dp
  17. 剑指offer3
  18. Dubbo配置优化
  19. Duplicate entry &#39;1&#39; for key &#39;PRIMARY&#39;
  20. Django基础五之django模型层(一)单表操作

热门文章

  1. 关于easyui checkbox 的一些操作处理
  2. WIN32通用控件之打开对话框获取文件路径
  3. 转载---sql之left join、right join、inner join的区别
  4. hdu 5171(矩阵快速幂,递推)
  5. 【互动问答分享】第7期决胜云计算大数据时代Spark亚太研究院公益大讲堂
  6. SPOJ NETADMIN - Smart Network Administrator(二分)(网络流)
  7. yum保留安装包方法,以及存放路径
  8. 20、Flask实战第20天:Flask上下文
  9. [AGC009C]Division into 2
  10. [Contest20180325]序列