hdoj-1856-More is better【并查集】

More is better

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)

Total Submission(s): 18427 Accepted Submission(s): 6779

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.

Author

[email protected]

Source

HDU 2007 Programming Contest - Final

Recommend

lcy | We have carefully selected several similar problems for you:
1325 1102 1301 1829 1811

#include<stdio.h>
#include<string.h>
#include<algorithm>
int maxn=-1;
using namespace std;
int root[10000001];
int res[10000001];
int find(int i){
	if(root[i]==i) return i;
	return root[i]=find(root[i]);
}
void unio(int x,int y){
	int p=find(x),q=find(y);
	if(p<=q) root[q]=p,res[p]+=res[q],maxn=max(maxn,res[p]);  // 在合并的时候顺便查找最大值,减少时间开销,否则超时!!!
	else root[p]=q,res[q]+=res[p],maxn=max(maxn,res[q]);
	return;
}
int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0){
			printf("1\n");
			continue;
		}
		memset(res,0,sizeof(res));
		int i,x,y,a,b;
		maxn=-1;
		for(i=1;i<=10000000;++i) root[i]=i,res[i]=1;
		for(i=1;i<=n;++i){
			scanf("%d%d",&a,&b);
			x=find(a);y=find(b);
			if(x!=y){
				unio(x,y);
			}
		}
		printf("%d\n",maxn);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-24 20:29:17

hdoj-1856-More is better【并查集】的相关文章

hdoj 2473 Junk-Mail Filter【并查集节点的删除】

Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7515    Accepted Submission(s): 2368 Problem Description Recognizing junk mails is a tough task. The method used here consists o

hdu 1856 More is better(并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 18985    Accepted Submission(s): 6990 Problem Description Mr Wang wants some

HDU 1856 More is better(并查集)

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 18523    Accepted Submission(s): 6814 Problem Description Mr Wang wants some boys

hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4384    Accepted Submission(s): 1673 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

hdoj 1878 欧拉回路(无向图欧拉回路+并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 思路分析:该问题给定一个无向图,要求判断该无向图是否存在欧拉回路:无向图判断存在欧拉回路的两个必要条件:该无向图为连通图且所有的结点的度数为偶数: 代码如下: #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAX_N = 1000 + 10

HDU 1856 More is better (并查集合并)

[题目链接]click here~~ [题目大意]这个题说的是有m个小朋友(boy),老师要选择尽可能多的小朋友,有一个规则,他们要有关系(比如a,b,c,如果a认识b,b认识c,那么a和c也算有关系)输入他们的关系图,询问最多可以选多少小朋友 [解题思路]把有关系的小朋友加入一个集合中,最后只要统计哪个集合的元素多就行了 代码: #include <bits/stdc++.h> using namespace std; const int N=100005; int father[N],nu

hdoj 3478 Catch(二分图判定+并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题即为判断该图是否为一个连通图且不为二分图: (1)二分图的性质:对于无向图G=(V, E),如果可以将图中的点划分为两个不相交的点集X与Y = V - X(V为点集),使得图中所有的边邻接的两个点分别存在集合X与集合Y中,则称该图G为二分图: (2) 二分图判定算法:二分图一种判定方法是给图中的每一

HDOJ 题目3367 Pseudoforest(并查集)

Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1932    Accepted Submission(s): 746 Problem Description In graph theory, a pseudoforest is an undirected graph in which every conne

hdoj 1232 畅通工程 【并查集】

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37403    Accepted Submission(s): 19808 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有

hdoj 1325 Is It A Tree? 【并查集】

做了一上午,终于ac了 wa了一次主要是忘了还有环!!! 主要是运用并查集知识,又复习了一次!! 思路:输入之后找能不能成环,成环就不是,其次还要判断是不是有两个父节点,如果有两个父节点也不是,之后就找相关的祖先就好了: 还要注意:如果只有一个节点,也是树,如果有两个或多个根节点也不是树:如果没有根节点也不是 链接http://acm.hdu.edu.cn/showproblem.php?pid=1325 代码 #include<stdio.h> int fat[1000]; int fath