其他分享
首页 > 其他分享> > [并查集]A. 【例题1】【模板】并查集

[并查集]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;
}

标签:10005,冰茶,int,查集,例题,模板
来源: https://www.cnblogs.com/unknown-future/p/14988935.html