Cod sursa(job #532199)

Utilizator cosmyoPaunel Cosmin cosmyo Data 11 februarie 2011 00:38:50
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <cstdio>
using namespace std;
const int N = 100005;
int RG[N], t[N], n, m;

int find(int x) {
	int R,  y;
	for(R = x; t[R] != R; R = t[R]);
	
	for(;x != t[x];) {
		y = t[x];
		t[x] = R;
		x = y;
	}

	return R;
}

void unite(int x, int y) {
	if(RG[x] > RG[y])
		t[y] = x;
	else
		t[x] = y;
	if(RG[x] == RG[y])
		++RG[y];
}

int main() {
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);
	int i, j, x, y, op;
	scanf("%d %d", &n, &m);

	for(i = 1; i<= n; ++i) {
		t[i] = i;
		RG[i] = 1;
	}

	for(i = 1; i <= m; ++i) {
		scanf("%d %d %d", &op, &x, &y);
		if(op == 2) {
			if(find(x) == find(y))
				printf("DA\n");
			else
				printf("NU\n");
		}

		else{
			if(find(x) == find(y))
				;
			unite(find(x), find(y));
		}
	}

	return 0;
}