Pagini recente » Cod sursa (job #2559197) | Cod sursa (job #1773610) | Cod sursa (job #2675383) | Cod sursa (job #2557184) | Cod sursa (job #832936)
Cod sursa(job #832936)
#include<cstdio>
using namespace std ;
int h[100],rad[101];
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;
}