More is better

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 327680/102400K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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. [/hint]
王老师要找一些男生帮助他完成一项工程。要求最后挑选出的男生之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个男生(最少挑一个)。
 思路:加一个数组记录数的元素个数;剩下的并差集解决;
代码:
 #include<stdio.h>
int relate[],num[];//num[]记录个数;
int min;
int find(int x){
int r=x;
while(r!=relate[r])r=relate[r];
int i=x,j;
while(i!=r)j=relate[i],relate[i]=r,i=j;
return r;
}
void initial(){
for(int i=;i<=;++i)relate[i]=i,num[i]=;
}
void merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
if(f1!=f2)relate[f1]=f2,num[f2]+=num[f1];
min=min>num[f2]?min:num[f2];
}
int main(){
int n,temp1,temp2;
while(~scanf("%d",&n)){
min=;initial();
while(n--){
scanf("%d%d",&temp1,&temp2);
merge(temp1,temp2);
}
printf("%d\n",min);
}
return ;
}

最新文章

  1. Python_Day5_迭代器、装饰器、软件开发规范
  2. visio个人专注
  3. 回头再看N层架构(图解)
  4. Cordova 打包 Android release app 过程详解
  5. unittest框架介绍
  6. kaili 2.0 metasploit连接postgres数据库
  7. 集合框架,ArrayList和Vector的区别,让arrayList线程安全的几种方案
  8. 数组名的含义.xml
  9. JS基于时间戳写的浏览访问人数
  10. 错误解决一_call time pass-by-reference removed
  11. Ajax改动购物车
  12. Struts2详细说明
  13. HDU--杭电--3415--Max Sum of Max-K-sub-sequence--暴力或单调队列
  14. [BZOJ1597]土地购买
  15. win10的power shell可以学习少部分linux命令_功能与cmd类似
  16. 纯CSS打造淘宝导航菜单栏
  17. SQLServer之删除存储过程
  18. Linux Tomcat8 启动堆内存溢出
  19. javaWeb的基础知识
  20. 1. dubbo概述

热门文章

  1. php5.3 appache phpstudy win7win8win10下 运行速度慢
  2. python高级编程之描述符与属性02
  3. Java动态 遍历List 时删除List特征元素 异常问题 及解决方案总结
  4. Java实现将指定目录内的指定类型的文件归类
  5. 图片轮播插件 Slides-SlidesJS-3
  6. MFC之树控件
  7. (原)在ubuntu 中安装 swi prolog 和 简单的使用
  8. java版-JQuery上传插件Uploadify使用实例
  9. MySQL数据库SQL层级优化
  10. HTML5 canvas 在线画笔绘图工具(一)