Pagini recente » Cod sursa (job #2297868) | Cod sursa (job #2831834) | Cod sursa (job #2877583) | Cod sursa (job #1168720) | Cod sursa (job #896018)
Cod sursa(job #896018)
#include<cstdio>
int t[100013],m,n,i,c,x,y;
inline void uneste(int x, int y)
{
int i,j;
i=j=0;
while(x!=t[x]){x=t[x];++i;}
while(y!=t[y]){y=t[y];++j;}
if(i<j)t[x]=y; else t[y]=x;
}
inline void gaseste(int x, int y)
{
while(x!=t[x])x=t[x];
while(y!=t[y])y=t[y];
if(x==y)printf("DA\n");else printf("NU\n");
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)t[i]=i;
for(i=0;i<m;++i)
{
scanf("%d%d%d",&c,&x,&y);
switch(c)
{
case 1:
{
uneste(x,y);
break;
}
case 2:
{
gaseste(x,y);
break;
}
}
}
return 0;
}