Cod sursa(job #1500373)

Utilizator theprdvtheprdv theprdv Data 11 octombrie 2015 20:21:05
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <cassert>

using namespace std;

#define MAXN 100002

int N, M, T[MAXN], Rank[MAXN];

inline int ROOT(int x) {
	int root = x, aux;

	for (; T[root]; root = T[root]);
	for (; T[x]; aux = T[x], T[x] = root, x = aux);

	return root;
}

inline void JOIN(int x, int y) {
	if (Rank[x] < Rank[y])
		T[x] = y;
	else {
		T[y] = x;
		if (Rank[x] == Rank[y]) ++Rank[x];
	}
}

int main()
{
	assert(freopen("disjoint.in", "r", stdin));
	freopen("disjoint.out", "w", stdout);
	int x, y, root1, root2, type;

	scanf("%d%d", &N, &M);

	while (M--) {
		scanf("%d%d%d", &type, &x, &y);
		root1 = ROOT(x), root2 = ROOT(y);

		if (type == 1) JOIN(root1, root2), ROOT(x), ROOT(y);
		else root1 == root2 ? puts("DA") : puts("NU");
	}

	return 0;
}