Cod sursa(job #390426)

Utilizator socheoSorodoc Ionut socheo Data 3 februarie 2010 18:50:25
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include<stdio.h>

int n,q,t[100021],r[100021],i,j,w,e;

void lipesc(int x,int y)
{	if(r[x]>r[y])
		t[y]=x;
	if(r[x]<r[y])
		t[x]=y;
	if(r[x]==r[y])
		{t[y]=x;
		r[x]++;
		}
}
int gasesc(int x)
{	int p=x,m,l=x;
	while(t[l]!=l)
		l=t[l];
	while(t[p]!=p)
	{	m=t[p];
		t[p]=l;
		p=m;
	}
	return l;
}

int main()
{	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	
	scanf("%d%d",&n,&q);
	
	for(i=1;i<=n;i++)
		{t[i]=i;
		r[i]=1;
		}
	
	for(int u=1;u<=q;u++)
		{ scanf("%d%d%d",&w,&e,&j);
			if(w==1)
			lipesc(e,j);
			else if(gasesc(e)==gasesc(j))
					printf("DA\n");
				else printf("NU\n");
				
		}
		
	return 0;}