Pagini recente » Cod sursa (job #292849) | Cod sursa (job #1061446) | Cod sursa (job #1404768) | Cod sursa (job #2523969) | Cod sursa (job #286506)
Cod sursa(job #286506)
#include <stdio.h>
long parinte[100000],rg[100000],x,rez,m,n,c,y,i,q;
long first(int x)
{long rez,aux;
rez=x;while(parinte[rez]!=rez)rez=parinte[rez];
while(parinte[x]!=x)
{aux=parinte[x];
parinte[x]=rez;
x=aux;
}
return rez;
}
void uneste(int x,int y)
{if(rg[x]>rg[y])parinte[y]=x;
else parinte[x]=y;
if(rg[x]==rg[y])rg[y]++;
}
int main()
{freopen("disjoint.in","r",stdin);freopen("disjoint.out","w",stdout);
scanf("%ld%ld",&n,&m);
for(i=1;i<=n;i++){rg[i]=1;parinte[i]=i;}
for(i=1;i<=m;i++)
{scanf("%ld%ld%ld",&q,&x,&y);
if(q==1)
{uneste(first(x),first(y));
}
else if(first(x)==first(y))printf("DA\n");else printf("NU\n");
}
fclose(stdin);fclose(stdout);
return 0;
}