Pagini recente » Cod sursa (job #2904614) | Cod sursa (job #1796913) | Cod sursa (job #1020247) | Cod sursa (job #3174845) | Cod sursa (job #832938)
Cod sursa(job #832938)
#include<cstdio>
using namespace std ;
int h[100005],rad[100005];
int find ( int x ) {
if(rad[x]==x)
return x;
return find(rad[x]);
}
void Union ( int x , int y ) {
if( h[x]==h[y] ) {
h[x]++;
rad[y]=x;
}
else
if ( h[x] > h[y] )
rad[y] =x;
else
rad[x]=y;
}
int main(){
freopen ("disjoint.in" , "r" , stdin);
freopen ("disjoint.out" , "w" , stdout);
int n,m,x,y,tx,ty,i,c;
scanf("%d" , &n);
for(i=1;i<=n;i++)
rad[i]=i;
scanf("%d" , &m);
for(i=1;i<=m;i++){
scanf("%d" , &c );
scanf("%d%d" , &x , &y );
if ( c==1 ){
tx=find(x);
ty=find(y);
Union(tx,ty);
}
else
if(find(x)==find(y))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}