查并集:107. 寻找存在的路径 (kamacoder.com)
#include<iostream>
#include<vector>
using namespace std;
vector<int> father(101, 0);
void init() {
for(int i = 1;i <= 101;i++) {
father[i] = i;
}
}
int find(int v) {
if(v == father[v]) return v;
else return father[v] = find(father[v]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return;
father[v] = u;
}
bool isSame(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return true;
else return false;
}
int main() {
int N, M;
cin >> N >> M;
init();
while(M--) {
int i, j;
cin >> i >> j;
join(i, j);
}
int start, end;
cin >> start >> end;
if(isSame(start, end)) cout << 1 << endl;
else cout << 0 << endl;
return 0;
}