More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 22283    Accepted Submission(s): 8106

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
题意:找最大的"朋友圈"吧。。
题解:数据量有点大。。我用hash离散了一下,然后再扫一遍。记录下每个集团的最大值然后再找的,然后还用了启发式并查集。可能有更优的方法吧!
注意的是输入0的时候要输出1,因为他自己也算一个。。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int M = ;
int Hash[M];
int cnt[N];
int father[N],dep[N]; int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int Union(int a,int b){
int x = _find(a);
int y = _find(b);
if(x==y) return ;
if(dep[x]==dep[y]){
father[x] = y;
dep[y]++;
}else if(dep[x]<dep[y]){
father[x] = y;
}else father[y]=x;
return ;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==) {
printf("1\n");
continue;
}
memset(Hash,-,sizeof(Hash));
memset(cnt,,sizeof(cnt));
for(int i=;i<=*n;i++){
father[i] = i;
dep[i]=;
}
int k=;
for(int i=;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
if(Hash[a]==-){
Hash[a]=++k;
}
if(Hash[b]==-){
Hash[b]=++k;
}
Union(Hash[a],Hash[b]);
}
for(int i=;i<=*n;i++){
cnt[_find(i)]++;
}
int Max = -;
for(int i=;i<=*n;i++){
Max = max(Max,cnt[i]);
}
printf("%d\n",Max);
}
}

最新文章

  1. Oracle 添加字段,并自增
  2. 【UOJ #147】【NOIP 2015】斗地主
  3. 电脑的f5刷新不了
  4. Python基础之函数等等
  5. [LeetCode]题解(python):055-Jump Game
  6. 【HDOJ】3309 Roll The Cube
  7. hdu 1728
  8. 与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)
  9. 算法:1!+(1!+3!)+(1!+3!+5!) + ( 1! + 3! + 5! + 7! + 9!)+....+(1!+3!+5!+ ... + m!)
  10. 在Android中使用枚举注解而不是枚举
  11. 自制tunnel口建虚拟专网实验
  12. Android View框架总结(七)View事件分发机制
  13. pywin32模块安装
  14. Sq lServer触发器的使用
  15. 揭示牌面使之升序 Reveal Cards In Increasing Order
  16. [Laravel] 08 - Auth &amp; Data Migration
  17. 工厂模式——Head First
  18. node.js cookie session使用教程
  19. 增量式PID的matlab实现
  20. java读取本地txt文件并插入数据库

热门文章

  1. Struts2---环境搭建及包介绍
  2. Java密码学综述---密码学基本功能
  3. 1196/P2323: [HNOI2006]公路修建问题
  4. Installation error: INSTALL_FAILED_CANCELLED_BY_USER
  5. ionic2升级到ionic3并打包APK
  6. SetConsoleCtrlHandler
  7. quartz 动态更改执行时间
  8. 【bzoj4419】[Shoi2013]发微博 STL-set
  9. 【bzoj2079】[Poi2010]Guilds 构造结论题
  10. 【Luogu】P4103大工程(虚树DP)