Pagini recente » Cod sursa (job #1847046) | Cod sursa (job #1887743) | Istoria paginii runda/tiberiu_popoviciu_pregatire/clasament | Cod sursa (job #2207966) | Cod sursa (job #669531)
Cod sursa(job #669531)
#include <cstdio>
using namespace std;
long txty,h[100001],t[100001],n,m,i,x,y,tx,ty,cod;
int find (int x)
{
for(;t[x]!=x;x=t[x]);
return x;
}
void Union (int x,int y)
{
if(h[x]>h[y])
t[y]=x;
else
if(h[x]==h[y])
{
h[x]++;
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",&cod,&x,&y);
tx=find(x);
ty=find(y);
if(cod==1)
{
Union(tx,ty);
}
if(cod==2)
{
if(tx==ty)
{
printf("DA\n");
}
else
printf("NU\n");
}
}
}