Cod sursa(job #1122879)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 25 februarie 2014 21:04:34
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include<cstdio>
using namespace std;
const int nmax = 100005;
int n,m,i,f[nmax],op,x,y;
int find(int x)
{
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
void unite(int x,int y)
{
    f[y]=x;
}
int main()
{
	freopen("disjoint.in","r",stdin);
	freopen("disjoint.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++) f[i]=i;
	for(;m;m--)
	{
	    scanf("%d%d%d",&op,&x,&y);
	    if(op==1) unite(find(x),find(y));
	    else
	    {
	        if(find(x)==find(y)) printf("DA\n");
	        else printf("NU\n");
	    }
	}
	return 0;
}