经典简单并查集题集



【HDU】1232 畅通工程

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232


题意: 找出最少要加几条路才可以让所有城市之间连通。

思路: 这是一道非常基础且经典的并查集题目。

  1. 相互连接的城市构成一个集合,因此只需要判断集合个数即可知道要修多少条路。
  2. 集合个数的判断可以根据每个集合只有一个根节点的特征,找n个数里有几个根节点,并减去1。
  3. 为什么减去1? 比如3个孤独的城镇互联,只需要两条路;同理三个集合之间关联也只需要两条路,所以是集合总数减1。

Code(C++):

#include <iostream>
#include <cstdio>
using namespace std;
int root[1010];
int n,m;

void init(int x){
	for(int i=1;i<=x;i++)
		root[i]=i;
}

int find(int x){
	while(root[x]!=x)
		x=root[x];
	return x;
}

void combine(int x,int y){
	int root1=find(x);
	int root2=find(y);
	if(root1 != root2)
		root[root1]=root2;
}

int main(){
	while(scanf("%d%d",&n,&m) && n){
		init(n);
		int a,b;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			combine(a,b);
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			if(root[i]==i)
				ans++;
		}
		printf("%d\n",ans-1);
	}
	return 0;
}


【HDU】1213 How Many Tables

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213


题意: 有n个客人,有m组两个客人之间的关系,代表两个客人之间相互认识,然后要给客人们安排桌子,客人们与其他人坐同一张桌子的条件是这张桌子上至少有一个人是他认识的,问最少要安排多少张桌子。

思路: 这也是一道经典并查集题目,跟上题几乎一样,就是要注意的就是,这里不是城市道路联通,而是求桌子数,并不联通,故最后的结果不需要减掉1。

Code(C++):

#include <iostream>
#include <cstdio>
using namespace std;
int root[1010];

int find(int x){
	while(root[x]!=x)
		x=root[x];
	return x;
}

void combine(int x,int y){
	int root1=find(x);
	int root2=find(y);
	if(root1 != root2)
		root[root1]=root2; 
}
int main(){
	int t;	cin>>t;
	int n,m;
	for(int k=1;k<=t;k++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			root[i]=i;
		int a,b;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			combine(a,b);
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			if(root[i]==i)
				ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}

Code(Java):

import java.util.Scanner;
public class Main {
	static int[] root = new int[1010];
	
	static int find(int x) {
		while(root[x]!=x) {
			x=root[x];
		}
		return x;
	}
	
	static void combine(int x,int y) {
		int root1 = find(x);
		int root2 = find(y);
		if(root1 != root2)
			root[root1]=root2;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int t = cin.nextInt();
		while(t-- >0){
			int n,m;
			n = cin.nextInt();
			m = cin.nextInt();
			for(int i=1;i<=n;i++)
				root[i]=i;
			for(int i=1;i<=m;i++) {
				int a = cin.nextInt();
				int b = cin.nextInt();
				combine(a,b);
			}
			int ans=0;
			for(int i=1;i<=n;i++) {
				if(root[i]==i)
					ans++;
			}
			System.out.println(ans);
		}
	}
}


【POJ】2524 Ubiquitous Religions

原题链接:http://poj.org/problem?id=2524


题意: 求学生信仰的宗教很多,求不同宗教个数。

思路: 简单并查集问题,模板题目。要注意的就是输出格式 “Case 1: 1”,当中的冒号是英文,且和后面的数有一个空格。如果改为中文的冒号,会WA。

Code(C++):

#include <iostream>
#include <cstdio>
using namespace std;
int root[100000];

int find(int x){
	while(root[x]!=x)
		x=root[x];
	return x;
}

void combine(int x,int y){
	int root1=find(x);
	int root2=find(y);
	if(root1 != root2)
		root[root1]=root2;
}

int main(){
	int t=0;
	int n,m;
	while(scanf("%d%d",&n,&m) && (n||m)){
		t++;
		for(int i=1;i<=n;i++)
			root[i]=i;
		int a,b;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			combine(a,b);
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			if(root[i]==i)
				ans++;
		}
		printf("Case %d: %d\n",t,ans);
	}
	return 0;
}

Code(Java)

import java.util.Scanner;
public class Main {
	static int[] root = new int[1000000];
	public static int fine(int x) {
		while(root[x] != x)
			x = root[x];
		return x;
	}
	public static void combine(int x,int y) {
		int root1 = fine(x);
		int root2 = fine(y);
		if(root1 != root2)
			root[root1] = root2;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int t=0;
		while(cin.hasNext()){
			t++;
			int n = cin.nextInt();
			int m = cin.nextInt();
			if(n==0 && m==0)
				break;
			for(int i=1;i<=n;i++)
				root[i]=i;
			for(int i=1;i<=m;i++){
				int a = cin.nextInt();
				int b = cin.nextInt();
				combine(a,b);
			}
			int ans=0;
			for(int i=1;i<=n;i++){
				if(root[i]==i)
					ans++;
			}
			System.out.println("Case " + t + ": " + ans);
		}
	}
}


【POJ】1611 The Suspects

原题链接:http://poj.org/problem?id=1611


题意: n个学生分为K组,然后告诉你每个组的人数和成员,找出和成员 a[0] 有直接或间接联系的同学的个数。

思路: 简单并查集问题,成员 a[0] 是感染者,和成员 a[0] 一组的都是怀疑对象。即找到哪些人和成员 a[0] 在一个集合里,就是看谁的根和成员 a[0] 的根一样。

Code(C++):

#include <iostream>
#include <cstring>
using namespace std;
int root[30005],a[30005];
int find(int x){
	/* 数据量比较大,要路径压缩,不然会WA
	while(root[x]!=x)
		x=root[x];
	return x;
	*/
	if(root[x]==x)
		return x;
	else{
		root[x]=find(root[x]);
	}
}
void combine(int x,int y){
	int root1=find(x);
	int root2=find(y);
	if(root1 != root2)
		root[root1]=root2;
}
int main(){
	int n,m;
	while(cin>>n>>m){
		if(n==0 && m==0)
			break;
		for(int i=0;i<n;i++)
			root[i]=i;
		int k;
		for(int i=0;i<m;i++){
			cin>>k>>a[0];
			for(int j=1;j<k;j++){
				cin>>a[j];
				combine(a[0],a[j]);
			}
		}
		int ans=0;
		for(int i=0;i<n;i++){
			if(find(i)==root[0])
				ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}


【POJ】2236 Wireless Network

原题链接:http://poj.org/problem?id=2236


题意: n 台电脑要完成连接,在距离 d 内才能完成连接。给出 n 个电脑的位置,如果再给出的是 O + 数字,表示激活该数字的电脑;如果是 S ,询问这两个电脑能否完成连接。

思路: 并查集的应用。先把所有电脑初始化为 false,表示没有激活。如果是 O,则标记为激活,同时把所有已激活且在范围内的电脑放在同一个根节点下。如果是 S,判断两台电脑的根节点是否一致。

Code(C++):

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int n,d;
int root[1010];
struct node{
	int x,y;
	bool open;
}arr[1010];
void init(int n){
	for(int i=1;i<=n;i++)
		root[i]=i;
}
int find(int x){
	if(root[x]==x)
		return x;
	else
		return root[x]=find(root[x]);
}
double dis(node a,node b){
	return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
void Union(int x){
	int a=find(x);
	for(int i=1;i<=n;i++){
		int b=find(i);
		if(arr[i].open && dis(arr[i],arr[x])<=d)
			root[b]=a;
	}
}
int main(){
	while(cin>>n>>d){
		init(n);
		for(int i=1;i<=n;i++){
			cin>>arr[i].x>>arr[i].y;
			arr[i].open=false;
		}
		getchar();
		char ch;
		while(cin>>ch){
			int x,y;
			if(ch=='O'){
				cin>>x;
				arr[x].open=true;
				Union(x);
			}else{
				cin>>x>>y;
				if(find(x)==find(y))
					cout<<"SUCCESS"<<endl;
				else
					cout<<"FAIL"<<endl;
			}
			getchar();
		}
	}
	return 0;
}


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值