[并查集]A. 【例题1】【模板】并查集


解析

这道题是一道冰茶姬的模板题, 不必多言, 直接上代码

Code

#include <bits/stdc++.h>
using namespace std;

int n, m, z, x, y; 
int f[10005];

int find (int x)
{
	while (x != f[x]) x = f[x] = f[f[x]];
	return x;
}

int main ()
{
	scanf ("%d%d", &n, &m);
	for (int i = 1; i <= n; ++ i) f[i] = i; 
	for (int i = 1; i <= m; ++ i)
	{
		scanf ("%d%d%d", &z, &x, &y);
		int a = find (x), b = find (y);
		if (z == 1) f[a] = f[b];
		else 
		{
			if (a == b) printf ("Y\n");
			else printf ("N\n");
		}
	}
	return 0;
}
posted @ 2021-07-09 07:41  unknown_future  阅读(50)  评论(0编辑  收藏  举报