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

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

More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)

Total Submission(s): 18656    Accepted Submission(s): 6860

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.
 

 

Author
lxlcrystal@TJU
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:            
//节点最多的并查集的节点数; (这段代码有一个优化, 根据数节点数不同在合并节点时进行区分)
ac 代码:
1 #include 
2 #include
3 using namespace std; 4 5 const int N = 10000002; 6 7 struct node 8 { 9 int parent;10 int cnt; 11 } boy[N];12 13 int n;14 int result;15 16 void init()17 {18 for(int i=0; i
boy[b].cnt)42 {43 boy[b].parent = a;44 boy[a].cnt += boy[b].cnt;45 if(result < boy[a].cnt)46 result = boy[a].cnt;47 }48 else49 {50 boy[a].parent = b;51 boy[b].cnt += boy[a].cnt;52 if(result < boy[b].cnt)53 result = boy[b].cnt;54 }55 }56 57 58 int main()59 {60 int a, b, i;61 // bool flag;62 while(cin >> n)63 {64 result = 1;65 init();66 while(n--)67 {68 scanf("%d %d", &a, &b);69 a = find(a);70 b = find(b);71 if(a != b)72 mercy(a, b);73 }74 cout << result << endl; 75 }76 return 0; 77 }

 //又敲了一遍。

1 #include 
2 #include
3 using namespace std; 4 const int N = 10000010; 5 int father[N], have[N], result; 6 void init() 7 { 8 result = 1; 9 for(int i=1; i
result)43 result = have[p];44 }45 }46 47 int main()48 {49 int a, b, n;50 while(~scanf("%d", &n))51 {52 init();53 while(n--)54 {55 scanf("%d %d", &a, &b);56 mercy(a, b);57 }58 printf("%d\n", result);59 }60 return 0;61 }

 

转载于:https://www.cnblogs.com/soTired/p/4686979.html

你可能感兴趣的文章
Prim 最小生成树算法
查看>>
金蝶结账的时候出现压缩账套失败 KIC_Compress.KD
查看>>
JavaScriptSerializer 序列化json 时间格式
查看>>
PyCharm 总结
查看>>
Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block
查看>>
ASP.NET Core 返回 Json DateTime 格式
查看>>
web安全实践(3)再谈基于http的服务器架构剖析
查看>>
SQL SERVER 2005 进行XML查询
查看>>
[CareerCup] 6.5 Drop Eggs 扔鸡蛋问题
查看>>
[Share]18个UI原稿图(包括twitter手稿)
查看>>
EXCEL的数据倒入到数据库
查看>>
浅析Java中的final关键字
查看>>
Spring有几种事务处理方式?举例说明
查看>>
php中mb_strlen,mb_substr根据中文长度截取字符串
查看>>
convert image to base64
查看>>
第 67 章 DDL
查看>>
CSS几个竖直与水平居中盒子模型
查看>>
希望早几年知道的5个Unix命令
查看>>
C# 控件不刷新问题
查看>>
Structs+Spring+Hibernate快速入门
查看>>