Pagini recente » Cod sursa (job #1029830) | Cod sursa (job #2302658)
#include<stdio.h>
int n,m,x,y,z,c,d,p[100001],r[100001];
int F(int x)
{
int r,y;
for(r=x;p[r]!=r;r=p[r]);
while(p[x]!=x)
y=p[x],p[x]=r,x=y;
return r;
}
int main()
{
freopen("disjoint.in","r",stdin),freopen("disjoint.out","w",stdout),scanf("%d%d",&n,&m);
for(x=1;x<=n;x++)
p[x]=x;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
if(x==1)
{
c=F(y),d=F(z);
if(r[c]>r[d])
p[d]=c;
else
p[c]=d;
if(r[c]==r[d])
r[d]++;
}
else
printf("%s\n",F(y)==F(z)?"DA":"NU");
}
}