本文主要是介绍代码随想录day55 寻找存在的路径,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码随想录day55 107. 寻找存在的路径
107. 寻找存在的路径
代码随想录
#include <vector>
#include <iostream>using namespace std;int find(vector<int> & father, int u) {return u == father[u] ? u : father[u] = find(father, father[u]);
}void init(vector<int> & father) {for (int i = 0; i < father.size(); i++) {father[i] = i;}
}bool isSame(vector<int> & father, int u, int v) {int s = find(father, u);int t = find(father, v);return s == t;
}void join(vector<int> & father, int u, int v) {int s = find(father, u);int t = find(father, v);if (s == t) return;father[t] = s;
}int main() {int N, M, s, t, source, destination;cin >> N >> M;vector<int> father(N + 1, 0);init(father);while(M--) {cin >> s >> t;join(father, s, t);}cin >> s >> t;if (isSame(father, s, t)) {cout << 1;} else {cout << 0;}return 0;
}
这篇关于代码随想录day55 寻找存在的路径的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!