并查集 入门典例 - 凌宸1642

并查集 入门典例 - 凌宸1642

1 通信系统

题目描述

​ 某市计划建设一个通信系统。按照规划,这个系统包含若干端点,这些端点由通信线缆链接。消息可以在任何一个端点产生,并且只能通过线缆传送。每个端点接收消息后会将消息传送到与其相连的端点,除了那个消息发送过来的端点。如果某个端点是产生消息的端点,那么消息将被传送到与其相连的每一个端点。
为了提高传送效率和节约资源,要求当消息在某个端点生成后,其余各个端点均能接收到消息,并且每个端点均不会重复收到消息
现给你通信系统的描述,你能判断此系统是否符合以上要求吗?

输入描述

​ 输入包含多组测试数据。每两组输入数据之间由空行分隔。
每组输入首先包含2个整数N和M,N(1<=N<=1000)表示端点个数,M(0<=M<=N*(N-1)/2)表示通信线路个数。
接下来M行每行输入2个整数A和B(1<=A,B<=N),表示端点A和B由一条通信线缆相连。两个端点之间至多由一条线缆直接相连,并且没有将某个端点与其自己相连的线缆。
当N和M都为0时,输入结束。

输出描述

​ 对于每组输入,如果所给的系统描述符合题目要求,则输出Yes,否则输出No。

样例输入

4 3
1 2
2 3
3 4

3 1
2 3

0 0

样例输出

Yes
No

思想: 其实只需要统计两个东西:

  • 该通讯系统中是否只有一个集合
  • 该集合中不存在环 (有环的话,会有结点收到重复信息)
#include<iostream>
using namespace std ;
//#define MAX 1010 
int father[1010] ; //记录结点 i 的父节点
bool isRoot[1010] ; // 记录是否是 根结点
int cnt = 0 ; // 记录环的数目
int findFather(int x){
	int v = x ;
	while(x != father[x]) x = father[x] ; // 找根结点 
	while(v != father[v]){ // 路径压缩 
		int z = v ;
		v = father[v] ;// 回溯 v 的父节点
		father[z] = x ;
	}
	return x ; // 返回根结点
}
void Union(int x , int y){
	int faX = findFather(x) ; // 找到 x 元素的 根结点
	int faY = findFather(y) ; // 找到 y 元素的 根结点
	if(faX != faY) father[faX] = faY ; // 如果 x y 所属不同集合,将他们合并成一个集合
	else cnt ++ ;  // 如果 x y 所属相同集合 , 则集合总有环 cnt 自增
}
void init(){
	for(int i = 0 ; i < 1010 ; i ++){
		father[i] = i ; // 初始化时,每个元素都是一个集合
		isRoot[i] = false ; // 每个元素均不是根结点
 	}
 	cnt = 0 ; // 环的个数初值为 0
}
int main(){
	int n , m ;
	while(scanf("%d%d" , &n , &m)!=EOF , n != 0 || m != 0){ // m , n 同时为 0 时结束循环
		init() ; // 初始化, 莫忘记
		int a , b  ;
		for(int i = 0 ; i < m ; i ++){
			scanf("%d%d" , &a , &b) ;
			Union(a , b ) ; // 将 a b元素所在集合合并
		}
		int ans = 0 ; // 记录通讯系统中 集合的个数
		for(int i = 1 ; i <= n ; i ++)
			isRoot[findFather(i)] = true ; // 如何使根结点
		for(int i = 1 ; i <= n ; i ++)
			if(isRoot[i]) ans ++ ;		// 如果该结点是 根结点,集合的数目加 1
		if(ans == 1 && cnt==0) printf("Yes\n") ; // 如果只有一个集合,并且没有环,输出 Yes
        else printf("No\n") ; // 否则输出 No
	} 
	return 0 ;
}

2 畅通工程

题目描述

​ 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

输入描述

​ 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。

注意:两个城市之间可以有多条道路相通,也就是说

3 3
1 2
1 2
2 1

这种输入也是合法的
当N为0时,输入结束,该用例不被处理。

输出描述

​ 对每个测试用例,在1行里输出最少还需要建设的道路数目。

样例输入

5 3
1 2
3 2
4 5
0

样例输出

1

思想: 就是看所给数据中有 n 个集合 , 为了变成一个集合,最少需要修 n - 1 条路。

#include<bits/stdc++.h>
using namespace std ;
int father[1010] ;
int n , m , a , b ;
int findFather(int x){
	int t = x ;
	while(x!=father[x]) x = father[x] ; // 寻找根结点 
	while(t != father[t]){ // 路径压缩 
		int z = t ;
		t = father[t] ; // 回溯 t 的父节点
		father[z] = x ;
	}
	return x ;  // 返回根结点 
}
void Union(int x , int y){
	int faX = findFather(x) ; // 找到 x 元素的 根结点
	int faY = findFather(y) ; // 找到 y 元素的 根结点
	if(faX != faY) father[faX] = faY ; // 如果 x y 所属不同集合,将他们合并成一个集合
}
void init(){
	for(int i = 0 ; i < 1010 ; i ++){
		father[i] = i ; // 初始化时,每个元素都是一个集合
	}
}
int main(){
	while(scanf("%d" , &n) != EOF , n){
		init() ; // 初始化 莫忘记
		scanf("%d" , &m) ;
		for(int i = 0 ; i < m ; i ++){
			scanf("%d%d" , &a , &b) ;
			Union(a , b) ; // 将 a b元素所在集合合并
		} 
		int ans = 0 ;
		for(int i = 1 ; i <= n ; i ++)
			if(i == father[i]) ans ++ ; // 统计集合的个数
		printf("%d\n" , ans - 1) ;	// 输出答案为 集合个数减 1
	}
	return 0;
}

3 How Many Tables

题目描述

​ Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

​ One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

​ For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

译:今天是伊格那丢的生日。他邀请了很多朋友。现在该吃晚饭了。伊格那丢想知道他至少需要多少张表。你要注意,不是所有的朋友都认识彼此,所有的朋友都不想和陌生人呆在一起。
这个问题的一个重要规则是如果我告诉你A知道B, B知道C,这意味着A, B, C认识,所以它们可以留在一个表中。
例如,如果我告诉你A知道B B知道C D知道E,那么A B C可以留在一个表中,而D E必须留在另一个表中。伊格那丢至少需要两张表格。

输入描述

​ The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

译:输入从一个整数T(1<=T<=25)开始,它表示测试用例的数量。然后是测试用例。每个测试用例从两个整数N和M开始(1<=N,M<=1000)。N表示好友的数量,从1到N标记,后面有M行。每一行由两个整数A和B(A!=B)组成,表示朋友A和朋友B认识。两种情况之间会有一个空白行。

输出描述

​ For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

译:对于每个测试用例,只需输出伊格内修斯至少需要多少个表即可。不要打印任何空格。

样例输入

2
6 4
1 2
2 3
3 4
1 4

8 10
1 2
2 3
5 6
7 5
4 6
3 6
6 7
2 5
2 4
4 3

样例输出

3
2

思想: 就是找集合的个数

#include<bits/stdc++.h>
using namespace std ;
int father[1010] ;
int t , n , m , a , b ;
int findFather(int x){
	int t = x ;
	while(x!=father[x]) x = father[x] ; // 寻找根结点 
	while(t != father[t]){ // 路径压缩 
		int z = t ;
		t = father[t] ;  // 回溯 t 的父节点
		father[z] = x ;
	}
	return x ;  // 返回根结点 
}
void Union(int x , int y){
	int faX = findFather(x) ; // 找到 x 元素的 根结点
	int faY = findFather(y) ; // 找到 y 元素的 根结点
	if(faX != faY) father[faX] = faY ; // 如果 x y 所属不同集合,将他们合并成一个集合
}
void init(){
	for(int i = 0 ; i < 1010 ; i ++){
		father[i] = i ;
	}
}
int main(){
	scanf("%d" , &t) ;
	while(t --){
		init() ;
		scanf("%d%d" , &n , &m) ;
		for(int i = 0 ; i < m ; i ++){
			scanf("%d%d" , &a , &b) ;
			Union(a , b) ; // 合并 a b 元素所在的集合
		} 
		int ans = 0 ;
		for(int i = 1 ; i <= n ; i ++)
			if(i == father[i]) ans ++ ; // 统计集合的个数
		printf("%d\n" , ans) ;	// 输出集合的个数
	}
	return 0;
}

4 More is better

题目描述

​ 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.

译:王先生想要一些男孩帮他做一个项目。因为这个项目相当复杂,男孩来的越多,就会越好。当然有一定的要求。王先生选了一间足够容纳男孩们的大房间。没有被选中的那个男孩必须马上离开房间。教室里有1000万名男生,一开始编号从1到1000万。经过王先生的选择,在这个房间里的任何两个孩子都应该是朋友(直接的或间接的),否则就只剩下一个男孩了。考虑到所有直接的朋友对,你应该决定最好的方式。

输入描述

​ 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)

译:输入的第一行包含一个整数n(0≤n≤100 000)-直接好友对的数量。下面n行每一行都包含一对数字a和B,中间用一个空格隔开,这表明a和B是直接的朋友。(A≠B, 1≤A, B≤1000000)

输出描述

​ The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

译:一行的输出仅包含一个整数,等于王先生可以保留的最大男孩数。

样例输入

3
1 3
1 5
2 5
4
3 2
3 4
1 6
2 6

样例输出

4
5

思想:其实很简单,就是将第一题中的 bool 数组 换成 int 型数组,并记录该集合的元素个数,然后输出最多元素的集合。

坑点:题目中最大数据说会给到 10000000 其实 10000 就能 AC 了 ,谜之操作 (用 10000000会时间超限)

#include<bits/stdc++.h>
using namespace std ;
int father[100010] ;
int isRoot[100010] ;
int n , m , a , b ;
int findFather(int x){
	int t = x ;
	while(x!=father[x]) x = father[x] ; // 寻找根结点 
	while(t != father[t]){ // 路径压缩 
		int z = t ;
		t = father[t] ;  // 回溯 t 的父节点
		father[z] = x ;
	}
	return x ;  // 返回根结点 
}
void Union(int x , int y){
	int faX = findFather(x) ; // 找到 x 元素的 根结点
	int faY = findFather(y) ; // 找到 y 元素的 根结点
	if(faX != faY) father[faX] = faY ; // 如果 x y 所属不同集合,将他们合并成一个集合
}
void init(){
	for(int i = 0 ; i < 100010 ; i ++){
		father[i] = i ;
		isRoot[i] = 0 ; // 初始时 0 
	}
}
int main(){
	while(scanf("%d" , &m) != EOF){
		init() ;
		for(int i = 0 ; i < m ; i ++){
			scanf("%d%d" , &a , &b) ;
			Union(a , b) ; // 合并 a b 元素所在的集合
		} 
		int ans = -1 ;
		for(int i = 1 ; i <= 100000 ; i ++){
            isRoot[findFather(i)] ++ ; // 对应根结点的元素个数自增 1
            if(isRoot[findFather(i)] > ans) // 如果当前根结点的元素个数比之前最大元素个数值大
				ans = isRoot[findFather(i)] ; // 更新最大元素个数值
        }
		printf("%d\n" , ans) ;	
	}
	return 0;
}

posted @ 2021-04-10 00:08  凌宸1642  阅读(67)  评论(0编辑  收藏  举报