Pagini recente » Cod sursa (job #2382267) | Cod sursa (job #2052103) | Cod sursa (job #1734909) | Cod sursa (job #838971) | Cod sursa (job #1039196)
#include <cstdio>
using namespace std;
long h[100001],t[100001],n,m,i,tip,mbr1,mbr2;
long Find (long x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void Union(long x,long y)
{
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
int main ()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%ld%ld",&n,&m);
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++)
{
scanf("%ld%ld%ld",&tip,&mbr1,&mbr2);
long cm1,cm2;
cm1=Find(mbr1);
cm2=Find(mbr2);
if(tip==2)
{
if(cm1==cm2)
printf("DA\n");
else
printf("NU\n");
}
else
Union(cm1,cm2);
}
return 0;
}