Cod sursa(job #1039199)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 22 noiembrie 2013 17:49:43
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
//infoarena--arh edu
#include <cstdio>

using namespace std;

int t[100001], h[100001];
int findset(int x)
{
	while(t[x]!=x)
		x=t[x];
	return x;
}
void unionset(int x, int y)
{
	if(h[x]==h[y])
	{
		h[x]++;
		t[y]=x;
	}
	else if(h[x]<h[y])
		t[x]=y;
	else t[y]=x;
}

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    int n, m, tip, x, y;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++)
	{
		t[i]=i;
	}
    for(int i=1;i<=m;i++)
	{
		scanf("%d", &tip);
		scanf("%d%d", &x, &y);
		if(tip==1)
		{
			unionset(t[x], t[y]);
		}
		else
		{
			if(findset(x)==findset(y))
				printf("DA\n");
			else printf("NU\n");
		}
	}
    return 0;
}