Pagini recente » Cod sursa (job #1471259) | Cod sursa (job #675175) | Cod sursa (job #3033227) | Cod sursa (job #862500) | Cod sursa (job #1107887)
#include <iostream>
#include <cstdio>
#define Nmax 100010
using namespace std;
int H[Nmax],Tata[Nmax];
int N,M;
int FindRoot(int x)
{
int R,aux;
R=aux=x;
for(;Tata[R]!=R;R=Tata[R]);
for(;Tata[x]!=x;)
{
aux=Tata[x];
Tata[x]=R;
x=aux;
}
return R;
}
void Unite(int x,int y)
{
int Rx=FindRoot(x);
int Ry=FindRoot(y);
if(H[Rx]>H[Ry])
Tata[Ry]=Rx;
else
Tata[Rx]=Ry;
if(H[Rx]==H[Ry])
H[Rx]++;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d %d",&N,&M);
int x,y,Op;
for(int i=1;i<=N;++i)
{
Tata[i]=i;
H[i]=1;
}
for(int i=0;i<M;++i)
{
scanf("%d %d %d",&Op,&x,&y);
if(Op==1)
{
Unite(x,y);
continue;
}
if(FindRoot(x)==FindRoot(y))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}