Cod sursa(job #1052159)

Utilizator kuramaKurama kurama Data 10 decembrie 2013 21:47:20
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <cstdio>
int n, m;
int a[100002];
int find(int x) {
	int y = x;
	while (x != a[x])
		x = a[x];
	while (y != a[x]) {
		int w = a[y];
		a[y] = x;
		y = w;
	}
	return x;
}
void join(int x, int y) {
	a[find(x)] = a[find(y)];
}
bool same(int x, int y) {
	if (find(x) == find(y))
		return true;
	return false;
}
int main() {
	//freopen("disjoint.in","r",stdin);
	//freopen("disjoint.out","w",stdout);
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		a[i] = i;
	for (int i = 1; i <= m; i++) {
		int q, x, y;
		scanf("%d%d%d", &q, &x, &y);
		if (q == 1)
			join(x, y);
		else
			printf("%s\n", (same(x, y) == true ? "DA" : "NU"));
	}
}