Pagini recente » Cod sursa (job #2634216) | Cod sursa (job #174483) | Cod sursa (job #979251) | Cod sursa (job #2557186) | Cod sursa (job #621981)
Cod sursa(job #621981)
#include<cstdio>
const int maxN=100005;
int n,m,tt[maxN],r[maxN];
int caut(int a)
{
int poz=a,b;
while(tt[poz]!=poz)
poz=tt[poz];
while(tt[a]!=a)
{
b=tt[a];
tt[a]=poz;
a=b;
}
return poz;
}
void work()
{
int o,x,y,i,j,p1,p2;
for(i=1;i<=n;i++)
{
tt[i]=r[i]=i;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&o,&x,&y);
p1=caut(x); p2=caut(y);
if(o==1)
{
if(r[p1]>r[p2])
tt[p2]=p1;
else tt[p1]=p2;
if(r[p1]==r[p2])
r[p2]++;
}
else
{
if(p1==p2)
printf("DA\n");
else printf("NU\n");
}
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
work();
return 0;
}