Pagini recente » Cod sursa (job #852566) | Cod sursa (job #2728459) | Cod sursa (job #489796) | Cod sursa (job #1636851) | Cod sursa (job #1184919)
#include <stdio.h>
#define NMAX 100020
int T[NMAX],R[NMAX],N,M,i,cod,x,y;
int find(int x)
{
int R,aux;
for (R=x;T[R]!=R;R=T[R]);
for (;T[x]!=x;)
{
aux=T[x];
T[x]=R;
x=aux;
}
return R;
}
void unite(int x,int y)
{
if (R[x]>R[y]) T[y]=x;
else T[x]=y;
if (R[x]==R[y]) ++R[y];
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d %d",&N,&M);
for (i=1;i<=N;i++) T[i]=i,R[i]=1;
for (i=1;i<=M;i++)
{
scanf("%d %d %d",&cod,&x,&y);
if (cod==1) unite(find(x),find(y));
else
if (find(x)==find(y)) printf("DA\n");
else printf("NU\n");
}
return 0;
}