90分求调
  • 板块P1551 亲戚
  • 楼主milestones
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/7/13 17:49
  • 上次更新2024/7/13 19:26:40
查看原帖
90分求调
1346392
milestones楼主2024/7/13 17:49
#include <bits/stdc++.h>
using namespace std;
int a[5005];
int n, m, p;
int x, y, kind = 0;
int main() {
	cin >> n >> m >> p;
	for (int i = 0; i < m; i++) {
		cin >> x >> y;
		if (a[x] == a[y] && a[x] == 0) {//判读新的类型
			kind += 1;
			a[x] = kind;
			a[y] = kind;
			continue;
		}
		//扩大家族
		if (a[x] != 0 && a[y] == 0) { a[y] = a[x]; continue; }
		if (a[y] != 0 && a[x] == 0) { a[x] = a[y]; continue; }

		//合并家族
		if (a[y] != 0 && a[x] != 0&&a[y]!=a[x]) {
			int z = a[y];
			int u = a[x];
			for (int j = 1; j <= n; j++) if (a[j] == z) a[j] = u;
			continue;
		}
	}

	for (int i = 0; i < p; i++) {
		cin >> x >> y;
		if (a[x] == a[y] && a[x] != 0)  cout << "Yes" << endl;
		else  cout << "No" << endl;
	}

	return 0;
}

好友发不出去,我帮发一下,请大佬帮忙看看!!!代表好友表达衷心的感谢。

2024/7/13 17:49
加载中...