当前位置: 首页 > news >正文

图论-并查集

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

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

.1.初始化

2.查询find

3.合并union

 

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

 

#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;
}

或者这样写 

#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;}

 

#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;
}

#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;
}

 

http://www.lryc.cn/news/258790.html

相关文章:

  • redis-学习笔记(Jedis 通用命令)
  • C语言:高精度乘法
  • UE4 Niagara学习笔记
  • 多维时序 | Matlab实现GA-LSTM-Attention遗传算法优化长短期记忆神经网络融合注意力机制多变量时间序列预测
  • LeetCode205. Isomorphic Strings
  • Event Driven设计模式
  • PostgreSql 设置自增字段
  • 什么是泊松图像混合
  • OpenAI 承认 ChatGPT 最近确实变懒,承诺修复问题
  • 创作活动(四十九)———低代码:美味膳食或垃圾食品?
  • 【DL-TensorFlow遇错】TensorFlow中遇错合集
  • pymysql代替mysqlclient,解决mysqlclient因版本不兼容无法安装成功而无法连接mysql的问题
  • uni-app 设置当前page界面进入直接变为横屏模式
  • Mysql的多表联合查询
  • Linux上使用Python的requests库进行HTTP请求
  • 图像处理领域的应用
  • MySQL笔记-第18章_MySQL8其它新特性
  • C语言—每日选择题—Day46
  • flex布局,换行的元素上下设置间距
  • 【智能家居】八、监控摄像采集、人脸识别比对进行开门功能点
  • golang的文件操作
  • 数据库版本管理框架-Flyway(从入门到精通)
  • 外网访问内网服务器使用教程
  • C# Dictionary 利用 ContainsValue 查询指定值是否已经存在
  • 招不到人?用C语言采集系统批量采集简历
  • HXDSP2441-Demo板
  • 静态路由的原理和配置
  • Ubuntu20.04降低linux版本到5.4.0-26-generic
  • C++ 类型萃取
  • 【JVM从入门到实战】(四)类的生命周期