Pagini recente » Cod sursa (job #356192) | Cod sursa (job #1966489) | Cod sursa (job #2432536) | Cod sursa (job #1350700) | Cod sursa (job #2429569)
#include <cstdio>
using namespace std;
int t[100005],h[100005];
int FindSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void UnionSet(int x,int y)
{
if(h[x]>h[y])
t[y]=x;
else
if(h[x]<h[y])
t[x]=y;
else
{
t[y]=x;
h[x]++;
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,x,y,caz,i,tx,ty;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&caz,&x,&y);
if(caz==1)
{
tx=FindSet(x);
ty=FindSet(y);
if(tx!=ty)
UnionSet(tx,ty);
}
else
if(FindSet(x)==FindSet(y))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}