Pagini recente » Cod sursa (job #1113569) | Cod sursa (job #1934063) | Cod sursa (job #2327862) | Cod sursa (job #3285785) | Cod sursa (job #714070)
Cod sursa(job #714070)
#include<cstdio>
using namespace std;
int t[100000],h[100000];
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 if(h[x]<h[y]) t[x]=y;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,x,y,i,cod,tx,ty;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&cod,&x,&y);
tx=find(x);
ty=find(y);
if (cod==1)
{
if(tx!=ty) Union(tx,ty);
}
else
{
if (tx==ty) printf("DA\n");
else printf("NU\n");
}
}
return 0;
}