A Bug‘s Life (并查集)

时间:2024-05-04 16:07:07

//新生训练

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5000;
int p[N], sz[N];
int n, m;
int find(int x)
{
	if (p[x] != x)
		p[x] = find(p[x]);
	return p[x];
}
int main()
{
	int T;
	scanf("%d", &T);
	for (int k = 1; k <= T; k++)
	{
		printf("Scenario #%d: ", k);
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= 2 * n; i++)
		{
			p[i] = i;
		}
		int flag = 0;
		for (int i = 1; i <= m; i++)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			int x1 = find(a), y1 = find(b);
			int x2 = find(a + n), y2 = find(b + n);
			if (x1 == y1)
				flag = 1;
			p[y2] = x1;
			p[x2] = y1;
		}
		if (flag)
			printf("Suspicious bugs found!\n\n");
			else printf("No suspicious bugs found!\n\n");
	}
	return 0;
}

~~~//仅当笔者个人备忘录使用。