Cod sursa(job #1906013)

Utilizator VAIonescuIonescu Vlad-Andrei VAIonescu Data 6 martie 2017 12:03:06
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <cstdio>
using namespace std;
const int nmax=100000;
int t[nmax+5];
int h[nmax+5];
int findMD(int x){
	while (x!=t[x]){
		x=t[x];
	}
	return x;
}
void unionMD(int tx,int ty){
	if (h[tx]==h[ty]){
		h[tx]++;
		t[ty]=tx;
	}else{
		if (h[tx]>h[ty]){
			t[ty]=tx;
		}else{
			t[tx]=ty;
		}
	}
}
int main()
{
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++){
		t[i]=i;
		h[i]=1;
	}
	for (int test=1;test<=m;test++){
		int cod,x,y;
		scanf("%d%d%d",&cod,&x,&y);
		if (cod==1){
			int tx=findMD(x);
			int ty=findMD(y);
			if (tx!=ty){
				unionMD(tx,ty);
			}
		}else{
			if (cod==2){
				int tx=findMD(x);
				int ty=findMD(y);
				if (tx==ty){
					printf("DA\n");
				}else{
					printf("NU\n");
				}
			}
		}
	}
    return 0;
}