杭电1856More is better
来源:互联网 发布:淘宝模特签约合同范本 编辑:程序博客网 时间:2024/05/29 15:42
求大神指点杭电1586more is better,答案对,就是超时(⊙o⊙)求指点
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 19936 Accepted Submission(s): 7313
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.#include <iostream>using namespace std;int arr[10000001] ;int num[10000001] ;int Max;inline int find(int r){ while(r!=arr[r]) r=arr[r]; return r;}inline void judge(int a,int b){ int x,y; x=find(a); y=find(b); if(x!=y) { arr[y]=x; num[x]=num[y]+num[x]; num[y]=num[x]; if(num[y]>Max) Max=num[y]; } }inline void Init(){ for(int i=1;i<= 10000001 ;i++) { arr[i]=i; num[i]=1; }}int main(){ int N,a,b; while (cin>>N) { Init(); Max=num[1]; while(N--) { cin>>a>>b; judge(a, b); } cout<<Max<<endl; } return 0;}
0 0
- 杭电1856-More is better
- 杭电 1856 more is better
- 杭电1856More is better
- 杭电1856More is better
- 【杭电】[1856]More is better
- HDOJ 1856 More is better 杭电 ACM
- 杭电1856More is better(并查集)
- hdu杭电1856 More is better【并查集】
- 杭电-1856 More is better (树的节点数)
- 【杭电1856】More is better找最大集合
- 杭电 More is better (DFS)
- 杭电1865 more is better
- More is better 1856
- 1856 More is better
- 杭电 hdu 1856 More is better (并查集)
- 并查集-杭电1856-More is better-难度1
- More is better(杭电1856)(并查集)
- 杭电 hdu 1856 More is better (并查集)
- 从技术经理的角度算一算,如何可以多快好省的做个App(一)
- 为dialog添加单选列表控件
- 刷题记录 2015.11.14
- QEMU+busybox 网络和用户配置的几个注意点
- Spark修炼之道(基础篇)——Linux大数据开发基础:第四节:Linux文件系统(二)
- 杭电1856More is better
- 个人收集的一些库、工具、技术介绍
- C语言、C++内存对齐问题详解
- LTView
- 更易操作的网页细节—background背景
- Android 5.0之后找不到apache类文件解决办法
- 【UML】 类图
- IOS应用显示不全
- Git在iOS开发中的使用