Pagini recente » Cod sursa (job #857423) | Cod sursa (job #3207521) | Cod sursa (job #467663) | Cod sursa (job #1371976) | Cod sursa (job #505626)
Cod sursa(job #505626)
#include<cstdio>
#define Nmax 100001
using namespace std;
void solve();
void unite(int x,int y);
void check(int x,int y);
int a[Nmax],lg[Nmax];
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
solve();
return 0;
}
void solve()
{
int N,M,x,y,i,cod;
scanf("%d%d",&N,&M);
for (i=1;i<=M;++i)
{
scanf("%d%d%d",&cod,&x,&y);
if (cod==1)
unite(x,y);
else
check(x,y);
}
}
void unite(int x,int y)
{
while (a[x]) x=a[x];
while (a[y]) y=a[y];
if (lg[x]==lg[y])
{
a[y]=x;
lg[x]++;
}
if (lg[x]>lg[y])
a[y]=x;
else
a[x]=y;
}
void check(int x,int y)
{
while (a[x]) x=a[x];
while (a[y]) y=a[y];
if (x==y)
printf("DA");
else printf("NU");
printf("\n");
}