Cod sursa(job #1258564)

Utilizator rcriptRazvan Popa rcript Data 9 noiembrie 2014 00:34:47
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <cstdio>
using namespace std;

#define nMax 100005

int T[nMax], R[nMax];

int N, M;

int find(int x){
	for(; x != T[x]; x = T[x])
		T[x] = T[T[x]];
	return x;
}

void join(int u, int v){
	if(R[u] > R[v])
		T[v] = u;
	else
		T[u] = v;
	if(R[u] == R[v])
		R[v] ++;
}

int main(){
	freopen("disjoint.in", "r", stdin);
	freopen("disjoint.out", "w", stdout);

	int x, y, type, u, v;
	
	scanf("%d %d", &N, &M);
	for(int i = 1; i <= N; ++ i)
		T[i] = i, R[i] = 1;
	
	while(M--){
		scanf("%d %d %d", &type, &x, &y);
		u = find(x);
		v = find(y);

		if(type == 1 && u != v)
			join(u, v);
		else if(type == 2)
			puts((u != v) ? "NU" : "DA");
	}

	fclose(stdin);
	fclose(stdout);	

	return 0;
}