20分求调,玄关
查看原帖
20分求调,玄关
1285611
JiangMzz楼主2024/8/10 18:18
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[10001];
int find(int x) {
	if (f[x] == x) return x;
	return f[x] = find(f[x]);
}
void join(int x, int y) {
	if (find(x) != find(y)) f[x] = y;
	return;
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		f[i] = i;
		rank[i] = 1;
	}
	for (int i = 1; i <= m; i++) {
		int opt, x, y;
		cin >> opt >> x >> y;
		if (opt == 1) {
			join(x, y);
		}
		else {
			int fx = find(x);
			int fy = find(y);
			if (fx == fy)
				cout << "Y" << endl;
			else 
				cout << "N" << endl;
		}
	}
	return 0;
}
2024/8/10 18:18
加载中...