Pagini recente » Cod sursa (job #786022) | Cod sursa (job #1703142) | Cod sursa (job #92780) | Cod sursa (job #206459) | Cod sursa (job #843204)
Cod sursa(job #843204)
#include <stdio.h>
#define MAXN 100001
int parent[MAXN];
int height[MAXN];
int N,M;
// a, b radacini
void reunion(int a, int b)
{
if( height[a] < height[b] ){
parent[a] = b;
}
else{
parent[b] = a;
if( height[a] == height[b] )
height[a]++;
}
}
int get_root(int a)
{
int root = a;
while( parent[root] != root )
root = parent[root];
parent[a] = root;
return root;
}
int main(int argc, char* argv[])
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d %d\n",&N,&M);
int i,tip,a,b;
for(i=1;i<=N;i++){
parent[i] = i;
height[i] = 1;
}
for(i=0;i<M;i++){
scanf("%d %d %d",&tip,&a,&b);
if(tip == 1){
int rootA = get_root(a);
int rootB = get_root(b);
if( rootA != rootB )
reunion(rootA,rootB);
}
else{
if( get_root(a) == get_root(b) )
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}