More is better(hdu 1856 计算并查集集合中元素个数最多的集合)
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 21167 Accepted Submission(s): 7720
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.
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
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>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
#define Max 10000000+5
int per[Max];
int ans[Max];
int num[Max];
bool vis[Max];
int n;
void init()
{
for(int i=;i<Max;i++)
per[i]=i;
}
int find(int a)
{
if(a==per[a])
return a;
return per[a]=find(per[a]);
}
int unite(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
per[a]=b;
return ;
}
int main()
{
int t,a,b;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
init();
int Maxn=,p=;
memset(vis,,sizeof(vis));
fill(ans,ans+Max,);
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
if(vis[a]==)
{
vis[a]=;
num[p++]=a;
}
if(vis[b]==)
{
vis[b]=;
num[p++]=b;
}
unite(a,b);
}
for(int i=;i<p;i++)
{
t=find(num[i]);
ans[t]++;
//cout<<t<<endl;
Maxn=max(Maxn,ans[t]);
}
printf("%d\n",Maxn);
}
}
More is better(hdu 1856 计算并查集集合中元素个数最多的集合)的更多相关文章
- POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...
- HDU1865--More is better(统计并查集的秩(元素个数))
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- Bipartite Graph hdu 5313 bitset 并查集 二分图
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...
- hdu 3536【并查集】
hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市. Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...
- HDU 1829 分组并查集
题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...
随机推荐
- DataTable中数据针对某列数据去重
//针对这些数据进行去重,取某一列就用这一列字段,把重复的去掉 DataSet ds = dao.get(); DataTable dt = ds.Table[0] DataView dv = dt. ...
- Ext4.0.7使用Ext.grid.ColumnModel报错:TypeError: Ext.grid.Model is not a constructor
代码如下: Ext.onReady(function(){ //定义列 var cm = new Ext.grid.ColumnModel([ {header: '编号', dataIndex: 'i ...
- poj2409 Let it Bead
Let it Bead Time Limit: 1000MS M ...
- Windows 1252和ISO 8859-1之间的区别(ISO 8859-1就是Latin-1,但1252与Latin1略有不同)
2.6.5. ANSI字符编码和Windows 1252 Windows为了支持英语和西欧字符,自己设计了一个编码,对应的在Code Page号是1252,被称为Windows 1252. Windo ...
- 《Programming WPF》翻译 第9章 6.我们进行到哪里了?
原文:<Programming WPF>翻译 第9章 6.我们进行到哪里了? 只有当任何内嵌控件都没有提供你需要的底层行为时,你将要写一个自定义控件.当你写一个自定义控件,你将要使用到依赖 ...
- c++ 07
一.多重继承 1)子类同时拥有两个或两个以上的基类,同时继承了所有基类的属性和行为. 销售员 经理 \ / 销售经理 汽车 客用特性 商用特性 \ | / ...
- STL容器是否是线程安全的
转载http://blog.csdn.net/zdl1016/article/details/5941330 STL的线程安全. 说一些关于stl容器的线程安全相关的话题. 一般说来,stl对于多线程 ...
- Objective-C中NSString和NSMutableString的基本用法
int main(int argc, const char * argv[]) { @autoreleasepool { //----------------NSString------------- ...
- MyWidget【简单自制控件】
#coding=gbk from PyQt4 import QtGui,QtCore import random class MyWidget(QtGui.QWidget): def __init__ ...
- proxy 利用get拦截,实现一个生成各种DOM节点的通用函数dom。
const dom = new Proxy({}, { get(target, property) { return function(attrs = {}, ...children) { const ...