博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu--1856 More is better
阅读量:5142 次
发布时间:2019-06-13

本文共 2494 字,大约阅读时间需要 8 分钟。

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

41 23 45 61 641 23 45 67 8

Sample Output

42

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. 题意:王老师要找一些男生帮助他完成一项工程。要求最后挑选出的男生之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个  男生(最少挑一个)。  代码:
1 #include
2 #include
3 #include
4 using namespace std; 5 int num[1000000],par[1000000]; 6 void init() 7 { 8 for(int i=1;i<1000000;i++) 9 { par[i]=i;10 num[i]=1; 11 }12 }13 int find(int x)14 {15 if(par[x]!=x)16 return par[x]=find(par[x]);17 return x;18 19 }20 void unite(int a,int b)21 {22 int fa=find(a);23 int fb=find(b);24 if(fa!=fb)25 {par[fa]=fb;26 num[fb]+=num[fa]; //这个地方注意,第一次num【fb】写成了b,27 28 }29 }30 int main()31 {32 int a,b,n; 33 while(scanf("%d",&n)!=EOF)34 {
if(n==0) 35 {printf("1\n");36 continue;37 38 } 39 init();40 int max=0;41 while(n--)42 {43 scanf("%d %d",&a,&b);44 if(a>max) max=a;45 if(b>max) max=b; 46 unite(a,b);47 }48 int m=0;49 for(int i=1;i<=max;i++)50 {
if(num[i]>m)51 m=num[i];52 53 } 54 printf("%d\n",m); 55 56 57 58 }59 return 0;60 }

 

转载于:https://www.cnblogs.com/hss-521/p/7277109.html

你可能感兴趣的文章
android 九宫加密记事本
查看>>
Vue自定义事件之左滑出现删除按钮
查看>>
Android NDK 同时编译多个模块
查看>>
结构体内嵌比较函数bool operator < (const node &x) const {}
查看>>
自我介绍
查看>>
最优二叉查找树
查看>>
HTML5权威指南 -- 创建HTML文档
查看>>
rsync数据同步工具应用指南
查看>>
SQL判断分段的连续值
查看>>
判断窗体 show完成
查看>>
基于STM32F4移植W5500官方驱动库ioLibrary_Driver(转)
查看>>
css:调整placeholder样式
查看>>
ACM 媛在努力 华山论剑
查看>>
转:MVC框架
查看>>
解决SMON_SCN_TO_TIME_AUX表损坏故障
查看>>
数据库表分区
查看>>
easy ui datatimebox databox 当前时间
查看>>
spark调优(二)-Apache Spark 内存管理详解
查看>>
NiuTrans 日记 1
查看>>
玩转visual studio系列之类设计图
查看>>