Pagini recente » Cod sursa (job #1073467) | Cod sursa (job #1724849) | Cod sursa (job #362189) | Cod sursa (job #2058675) | Cod sursa (job #1660717)
#include <stdio.h>
#include <stdlib.h>
int f(int *, int);
void unite(int *, int *, int, int);
int main(){
int n, m, c, x, y, *t, *r, i;
freopen("disjoint.in","r", stdin);
freopen("disjoint.out","w", stdout);
scanf("%i%i",&n,&m);
t = (int *) malloc((n + 1) * sizeof(int));
r = (int *) malloc((n + 1) * sizeof(int));
for(i = 1; i <= n; ++i){
t[i] = i;
r[i] = 1;
}
while(m){
scanf("%i %i %i", &c, &x, &y);
if(c == 2){
if(f(t, x) == f(t, y)){
printf("DA\n");
}
else{
printf("NU\n");
}
}
else{
unite(t, r, x, y);
}
--m;
}
return 0;
}
int f(int *t, int x){
int tx = x, y;
while(t[tx] != tx){
tx = t[tx];
}
while(t[x] != x){
y = t[x];
t[x] = tx;
x = y;
}
return tx;
}
void unite(int *t, int *r, int x, int y){
if(r[x] < r[y]){
t[x] = y;
}
else{
t[y] = x;
}
if(r[x] == r[y]){
r[y]++;
}
}