当前位置: 首页> 财经> 产业 > 代码随想录第六十天打卡

代码随想录第六十天打卡

时间:2025/7/11 0:12:53来源:https://blog.csdn.net/2303_79574965/article/details/140929490 浏览次数:0次

并查集理论基础

并查集理论基础很重要,明确并查集解决什么问题,代码如何写,对后面做并查集类题目很有帮助。

并查集理论基础 | 代码随想录

寻找存在的路径

并查集裸题,学会理论基础后,本题直接可以直接刷过

代码随想录

#include <iostream>
#include <vector>
using namespace std;
const int n = 1000;
vector<int>father(n, 0);
void init() {for (int i = 0; i < n; i++) {father[i] = i;}
}
int find(int n) {return father[n] == n ? n : father[n] = find(father[n]);
}
int isSame(int n, int v) {n=find(n);v=find(v);return n == v;
}
void join(int n, int v) {n=find(n);v=find(v);if (n == v)return;father[v] = n;
}
void solve() {int n, m;cin >> n >> m;for (int i = 0; i < m; i++) {int a, b;cin >> a >> b;join(a, b);}int source, destination;cin >> source >> destination;cout << isSame(source, destination) << endl;
}
int main() {init();solve();return 0;
}

关键字:代码随想录第六十天打卡

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: