图论-并查集

**并查集(Union-find Sets)**是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题.一些常见的用途有求连通子图,求最小生成树Kruskal算法和最近公共祖先(LCA)等.

并查集的基本操作主要有:

.1.初始化

2.查询find

3.合并union

一般我们都会采用路径压缩 这样效率更加高

cpp 复制代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define MAXN 20001
int fa[MAXN];
void init(int n) {
	for (int i = 1; i <= n; i++) {
		fa[i] = i;
	}//初始化
}
int find(int x) {
	if (x == fa[x]) {
		return x;

	}
	else {
		fa[x] = find(fa[x]);//路径压缩 也就是一直找到祖先
		return fa[x];
	}
}
void unionn(int i, int j) {
	int i_fa = find(i);//找到i的祖先
	int j_fa = find(j);//找到j的祖先
	fa[i_fa] = j_fa;//i的祖先指向j的祖先 反过来也可以
}
int main() {
	int n, m, x, y, q;
	scanf("%d", &n);
	init(n);
	scanf("%d", &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &x, &y);
		unionn(x, y);
	}
	scanf("%d", &q);
	for (int i = 1; i <= q; i++) {
		scanf("%d%d", &x, &y);
		if (find(x) == find(y)) {
			printf("Yes\n");
		}
		else {
			printf("No\n");
		}

	}


	return 0;
}

或者这样写

cpp 复制代码
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 20010;

int n, m;
int p[N];
int find(int x) {
	if (p[x] != x)p[x] = find(p[x]);
	return p[x];
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) p[i] = i;
	while (m--) {
		int a, b;
		scanf("%d%d", &a, &b);
		p[find(a)] = find(b);//合并 a->b
	}
	scanf("%d,&m");
	while (m--) {

		int a, b;
		scanf("%d%d", &a, &b);
		if (find(a) == find(b))puts("yes");
		else puts("no");
	}
	return 0;
	
}
cpp 复制代码
#include<iostream>
using namespace std;

const int N = 10010;

int n, m;
int p[N];

int find(int x) {
	if (p[x] != x)p[x] = find(p[x]);
	return p[x];
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) p[i] = i;
	char op[2];//读入操作的字符串  因为字符串后面有'\0'所以要存多一位
	while (m--) {
		int a, b;
		scanf("%s%d%d",&op ,&a, &b);
		if(*op=='M')p[find(a)] = find(b);//合并
		else {
			if (find(a) == find(b)) {
				puts("Yes");
			}
			else {
				puts("No");
			}
		}

	}

	return 0;
}

cpp 复制代码
#include<iostream>
using namespace std;
const int N = 10010;

int n, m;
int p[N], s[N];

int find(int x) {
	if (p[x] != x)p[x] = find(p[x]);
	return p[x];
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) p[i] = i, s[i] = 1;
	while (m--)
	{
		char op[3];
		int a, b;
		scanf("%s", &op);
		if (*op == 'C') {
			scanf("%d%d", &a, &b);
			a = find(a), b = find(b);
			if (a != b) {//如果相等证明他们在同一个祖先中
				s[b] += s[a];
				p[a] = b;
			}
			else if (*op == 'Q1') {
				scanf("%d%d", &a, &b);
				if (find(a) == find(b)) {
					puts("Yes\n");
				}
				else {
					puts("No\n");
				}
			}
			else {
				scanf("%d", &a);
				printf("%d\n", s[find(a)]);
			}
		}
	}
	return 0;
}
相关推荐
Tairitsu_H4 天前
[C++高阶数据结构·图论下] 最短路径:为什么“挑最小的”一定正确?
数据结构·c++·图论
Tairitsu_H4 天前
[C++高阶数据结构·图论上] 最小生成树:为什么“选边”和“选点”是两条路?
数据结构·c++·图论·最小生成树
Mortalbreeze10 天前
0 - 1 BFS 算法详解:从基本原理到经典题目建模
数据结构·c++·算法·图论·宽度优先
一去不复返的通信er10 天前
力扣-图论-岛屿数量
算法·leetcode·图论
春弦_13 天前
自用算法整理二
数据结构·算法·图论
lvwangshu14 天前
P8981 「DROI」Round 1 距离 题解
动态规划·图论·题解·dp·树形dp·技巧·换根dp·性质题·思维好题
海海不掉头发15 天前
图论与应用_课堂笔记
笔记·图论
云淡风轻~窗明几净15 天前
角谷猜想最接近严格证明的测试 Version1.1.1
人工智能·算法·机器学习·图论
云淡风轻~窗明几净15 天前
角谷猜想与百度AI的对话dialog(原始稿)
人工智能·算法·机器学习·图论
罗湖老棍子16 天前
【例 1】Sightseeing Trip(信息学奥赛一本通- P1494)(洛谷-P10927)
数据结构·算法·图论·最短路·floyd·最小环