HDU
来源:互联网 发布:淘宝上怎么购买呼死你 编辑:程序博客网 时间:2024/04/25 16:10
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 30751 Accepted Submission(s): 10922
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.
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
42HintA 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
并查集水题,需要注意的是:当N = 0的时候代表所有人之间都没有联系,所有最终只会留下一个人。
有两种解决的办法:
1、按照求森林数的方法。在最后对每个集合的元素数量求值
#include <iostream>#include <cstdio>#include <cstring>#include <set>using namespace std;const int MAXN = 10000000 + 5;int MAX = 0;int MIN = 0x7fffffff;int degree[MAXN];int parent[MAXN];set<int> cache;void initSet(){ MAX = 0; MIN = 0x7fffffff; memset(degree,0,sizeof(degree)); for(int i = 0; i < MAXN; i ++) parent[i] = i;}int find(int x){ if(x != parent[x]) return parent[x] = find(parent[x]); else return parent[x];}void unionSet(int a,int b){ int x = find(a); int y = find(b); if(x != y) parent[x] = y;}int main(){ int N; while(scanf("%d",&N) != EOF){ if(!N){ cout <<1 <<endl; continue; } initSet(); int u,v; for(int i = 0; i < N; i ++){ scanf("%d%d",&u,&v); MAX = max(MAX,u); MAX = max(MAX,v); MIN = min(MIN,u); MIN = min(MIN,v); cache.insert(u); cache.insert(v); unionSet(u,v); } for(int i = MIN; i <= MAX; i ++){ degree[find(i)] ++; } int max = 0; for(int i = MIN; i <= MAX; i ++){ if(max < degree[i] && cache.count(i)) max = degree[i]; } cout << max<<endl; } return 0;}2、直接在union操作的时候将集合元素数量由下级累加给上级
#include <iostream>#include <cstdio>#include <cstring>#include <set>using namespace std;const int MAXN = 10000000 + 5;int MAX = 0;int MIN = 0x7fffffff;int degree[MAXN];int parent[MAXN];set<int> cache;void initSet(){ MAX = 0; MIN = 0x7fffffff; for(int i = 0; i < MAXN; i ++){ parent[i] = i; degree[i] = 1; }}int find(int x){ if(x != parent[x]) return parent[x] = find(parent[x]); else return parent[x];}void unionSet(int a,int b){ int x = find(a); int y = find(b); if(x != y){ parent[x] = y; degree[y] += degree[x]; }}int main(){ int N; while(scanf("%d",&N) != EOF){ if(!N){ cout <<1 <<endl; continue; } initSet(); int u,v; for(int i = 0; i < N; i ++){ scanf("%d%d",&u,&v); MAX = max(MAX,u); MAX = max(MAX,v); MIN = min(MIN,u); MIN = min(MIN,v); cache.insert(u); cache.insert(v); unionSet(u,v); } int maxcnt = 0; for(int i = MIN; i <= MAX; i ++){ if(maxcnt < degree[i] && cache.count(i)) maxcnt = degree[i]; } cout << maxcnt<<endl; } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 特殊权限set_uid set_gid stick_bit 软硬链接
- Java ActiveMQ简介以及springboot集成activeMQ实现消息队列监听以及实现MQ延迟
- java常见代码面试题
- XShell实现Windows主机与Linux云服务器之间的文件传输
- SSH2整合:'hibernate.dialect' not set 解决方案
- HDU
- pythonic之路(二)
- idea中刷新页面无反应
- 找出不是两个数组共有的元素
- Metasploit 攻击
- 无界面 socket通信模拟在线评测
- 解决Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
- MyBatis DAO层有多个参数的处理
- consul UI用127可以访问,指定ip无法访问