HDUOJ----More is better(并查集)
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 10473 Accepted Submission(s): 3877
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.
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
2
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<cstdio>
#define maxn 100001
using namespace std;
int father[maxn+],rank[maxn+];
void inite(int n)
{
for(int i=;i<=n;i++)
{
father[i]=i;
rank[i]=;
}
} int findset(int x)
{
if(x!=father[x])
{
father[x]=findset(father[x]);
}
return father[x];
} void unset(int x,int y)
{
x=findset(x);
y=findset(y);
if(x==y) return ;
if(rank[x]>rank[y])
{
father[y]=father[x];
rank[x]+=rank[y];
}
else
{
father[x]=father[y];
rank[y]+=rank[x];
}
} int main()
{
int n,a,b,i,max;
while(scanf("%d",&n)!=EOF)
{
inite(maxn);
for(i=;i<n;i++)
{
scanf("%d %d",&a,&b);
unset(a,b);
}
for(i=;i<=maxn;i++)
{
if(i==||max<rank[i])
max=rank[i];
}
printf("%d\n",max);
}
return ;
}
HDUOJ----More is better(并查集)的更多相关文章
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
随机推荐
- Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善
1.struts2 目前是2.3版本,下载地址http://struts.apache.org/download.cgi struts2包 struts2-core-2.3.16.3.jar stru ...
- pytorch 学习问题
https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#sphx-glr-beginner-blitz-n ...
- Windows下安装和配置Apache2.2.22服务器+PHP5+Mysql5
接下来,笔者将WindowsXP(Sp3)下,搭建和配置php运行环境做一个简单的介绍: 1.安装准备 1.1Apache下载,官方地址(http://httpd.apache.org/),此处笔者下 ...
- Bootstrap学习js插件篇之下拉菜单
案例 通过此插件可以为几乎所有东西添加下拉菜单,包括导航条.标签页.胶囊式按钮. 用于导航条 导航条分为四个部分.第一部分导航头,第二部分导航列,第三部分form查询表单,第四部分导航列. <n ...
- 【leetcode】 9. palindrome number
@requires_authorization @author johnsondu @create_time 2015.7.13 9:48 @url [palindrome-number](https ...
- 【Docker】容器、虚拟机与Docker概念全解析
导读 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.本文立足于新手,从容器和虚拟机两个大 ...
- 解决 ASP.NET 编辑错误"CS0006: 未能找到元数据文件C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll"
问题背景: 公司最近给我配置了一台新Windows 7旗舰版的电脑,这几天一直在迁移文件,因为新电脑上安装Sqlserver r2失败,解决方法是要安装一个800+MB的安装包 由于最近手上事情比较多 ...
- json数据的用法
json数据在后台跟前台传递数据使用是非常多站点开发者再熟悉只是的数据格式了,可是呢在这之前肯定有不少人还不知道json数据是怎么使用的, {"name":"01&quo ...
- [Javascript] Coding interview problem: Scheduler functional way
Implement a job scheduler which takes in a function f and an integer n, and calls f after nmilliseco ...
- UE如何使用正则表达式
1 基本概念 元字符: 元字符 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符() \d 匹配数字 \b 匹配单词的开始或结束 \W 匹配任意不是字母 ...