More is better

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

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.

 
 
这个题的意思就是输入是好友的两人,两好友的盆友具有传递性!最后输出互相是好友最多的一群有多少人?
 
 
用并查集,具体来看注释部分!
 
 
 #include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 100000
using namespace std;
int ran[maxn],per[maxn],via[maxn],num[maxn],n,sum;
void init()
{
int i;
for(i=;i<=maxn;i++)
{
per[i]=i;
num[i] = ;//记录每个根节点所包含的子节点
}
}
int find(int x)//查找函数
{
int t=x;
while(t!=per[t])
t=per[t];
int i=x,j;
while(i!=t)
{
j=per[i];
per[i]=t;
i=j;//压缩路径
}
return t;
}
void join(int x,int y)//合并函数,合并图
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx] = fy;
num[fy] += num[fx];
sum = max(sum, num[fy]);//求出各个树中子节点最多的
}
else
return ;
}
int main ()
{
int a,b,i,w,n;
while(scanf("%d",&n)!=EOF)
{
if(n == ){
printf("1\n");//特殊坑爹数据
continue;
}
init();
w=n;
sum = ;
while(w--)
{
scanf("%d%d",&a,&b);
via[a]=;
via[b]=;
join(a,b);
}
printf("%d\n", sum);
}
return ;
}
 
 
 
 
 
 
 

More is better--hdu1856(并查集)的更多相关文章

  1. hdu1856 并查集

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1856/ 题目就是要求并查集中各树的大小的最大值,我们只要在根节点处存树的大小就可以,合并也是合并根节点的数,最后 ...

  2. 【HDU1856】More is better(并查集基础题)

    裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...

  3. hdu-1856 More is better---带权并查集

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1856 题目大意: 一个并查集 计算每个集合的元素 找出元素最多的那个集合,输出元素的个数 解题思路: ...

  4. hdu1856 More is better (并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  5. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

  6. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  7. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  8. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  9. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  10. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

随机推荐

  1. android listview Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

    android listview 适配器在多种类型viewType报错: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; ...

  2. PyQuery查询html信息

    以下代码主要演示使用pyquery进行对html文件的解析,包括设定编码,对子块进行查询等操作: from pyquery import PyQuery as pq import os from lx ...

  3. 【1】python核心编程 第三章

    1.继续( \ ) 有两种例外情况一个语句不使用反斜线也可以跨行.在使用闭合操作符时,单一语句可以跨多行,例如:在含有小括号.中括号.花括号时可以多行书写.另外就是三引号包括下的字符串也可以跨行书写 ...

  4. 使用PHP从web访问mysql数据库

    一. web数据库构架的工作原理 1. 用户由浏览器发出HTTP请求,请求特定的web页面. 2. web服务器接受接收到对特定页面的请求,检索相应文件,并将其传递给php引擎处理. 3. php引擎 ...

  5. jQuery插件之artDialog

    artDialog是一个非常强大的弹出框插件.默认有两个版本,一个是jQuery版,一个是javascript版.功能非常多,而且使用非常简单.不写了,直接贴上官网的预览运行地址,以后用得着的时候去那 ...

  6. Css 小技巧总结

    相对偏移 指定left top等属性就能够够完美控制一个元素的位置 如: position:relative; left:2px; 今天遇到一个很好玩的问题,当两个并排浮动框,当一个框的长度太大时就会 ...

  7. 从Hello, world开始认识IL <第一篇>

    IL代码分析方法 Hello, world历史 .NET学习方法论 1.引言 1988年Brian W.Kernighan和Dennis M.Ritchie合著了软件史上的经典巨著<The C ...

  8. 【转】飞凌嵌入式(Forlinx)TE/OK6410内核编译:“make: arm-none-linux-gnueabi-gcc:命令未找到”

    原文网址:http://www.xuebuyuan.com/1104711.html Ubuntu10.04下编译飞凌嵌入式(Forlinx)TE/OK6410开发板提供的内核2.6.36 本以为按照 ...

  9. 2015第10周四-CSS小结

    这两天做前台页面发现个人在CSS前端方法很多基础知识都忘了,晚上又搜索学习了下,把相关内容摘录总结. CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. selector {declara ...

  10. Linux下程序崩溃,ulimit,coredump,gdbserver

    操作系统:Ubuntu10.04 前言:    在程序崩溃后,如何快速定位问题.    以下方法适用于开发调试阶段,不太适用成品.    本文着眼于嵌入式,PC方面更简单.    核心:gdbserv ...