HDU1856--More is better
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 9710 Accepted Submission(s): 3573
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
HDU 2007 Programming Contest - Final
Recommend
lcy
简单并查集,题目意思是算数目最多的一个集合的个数

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<cmath> 6 #include<map> 7 using namespace std; 8 9 int parent[10000001]; 10 int n; 11 12 void Ufset() 13 { 14 int i; 15 for(i=1;i<=1000000;i++) 16 { 17 parent[i]=-1; 18 } 19 } 20 21 int Find(int x) 22 { 23 int s; 24 for(s=x;parent[s]>0;s=parent[s]){} 25 while(s!=x) 26 { 27 int temp=parent[x]; 28 parent[x]=s; 29 x=temp; 30 } 31 return s; 32 } 33 34 void Union(int R1,int R2) 35 { 36 int r1=Find(R1),r2=Find(R2); 37 int temp=parent[r1]+parent[r2]; 38 parent[r1]=r2; 39 parent[r2]=temp; 40 } 41 42 int main() 43 { 44 int c; 45 while(scanf("%d",&c)!=EOF) 46 { 47 Ufset(); 48 map<int,int>m; 49 int flag=1; 50 int k=c; 51 while(c--) 52 { 53 int u,v; 54 scanf("%d%d",&u,&v); 55 if(!m[u])m[u]=flag++; 56 if(!m[v])m[v]=flag++; 57 if(Find(m[u])!=Find(m[v])) 58 Union(m[u],m[v]); 59 } 60 int mmax=0; 61 for(int i=1;i<flag;i++) 62 { 63 if(mmax>parent[i]) 64 { 65 mmax=parent[i]; 66 } 67 } 68 if(k) 69 printf("%d\n",-mmax); 70 else 71 printf("1\n"); 72 } 73 return 0; 74 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法