【并查集专题】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): 34361    Accepted Submission(s): 12181

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.

思路

求最大连通块的元素个数:

我的最初思路是利用isRoot数组,用hash的方式,记录下每个连通块的元素个数;

不过这种方法时间、和内存都消耗的很大,最后超时了(TLE)

#include 
#include 
#include  
#include  
using namespace std; 
const int nmax=10000000+10; 
int father[nmax];
int isRoot[nmax];
int findFather(int u){
	if(u==father[u]) return u;
	else{
		int f=findFather(father[u]);
		father[u]=f;
		return f;
	}
}
void Union(int u,int v){
    int fu=findFather(u);
	int fv=findFather(v);
	if(fu!=fv){
		father[fu]=fv;
	} 
} 
void init(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
		isRoot[i]=0;
	}
}
int main(int argc, char** argv) {
	int m;//m条边 
	while(cin>>m){
		memset(father,0,sizeof(father));
		memset(isRoot,0,sizeof(isRoot));
		init(nmax); 
		int u,v;
		for(int i=0;i>u>>v;
			Union(u,v);
		}
		for(int i=1;i<=nmax;i++){
			isRoot[findFather(i)]++;
		}
		int ans=-1;
		for(int i=1;i<=nmax;i++){
			if(ans

于是就要改变Union的策略,在合并集合元素时,用sum数组同步更新计算最大集合元素值。

以下是:

AC Code

#include 
#include 
#include  
#include  
using namespace std; 
const int nmax=10000000+10; 
int father[nmax];
int sum[nmax];//sum[i]表示:节点i所在的连通块的节点个数 
int ans=0;      //最大连通块的节点个数
//int isRoot[nmax];
int findFather(int u){
	if(u==father[u]) return u;
	else{
		int f=findFather(father[u]);
		father[u]=f;
		return f;
	}
}
void Union(int u,int v){
    int fu=findFather(u);
	int fv=findFather(v);
	if(fu!=fv){
		father[fv]=fu;//fu是fv的根 
		sum[fu]=sum[fu]+sum[fv];
		ans=max(ans,sum[fu]); 
	} 
} 
void init(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
		sum[i]=1;//每个节点权值为1 
	}
}
int main(int argc, char** argv) {
	int m;//m条边 
	ios_base::sync_with_stdio(false); 
	while(cin>>m){
		if(m==0){
			cout<<"1"<>u>>v;
			Union(u,v);
		}
	
		cout<

 

你可能感兴趣的:(机试)