Pagini recente » Cod sursa (job #820068) | Cod sursa (job #2382076) | Cod sursa (job #962820) | Cod sursa (job #1015757) | Cod sursa (job #273214)
Cod sursa(job #273214)
#include<stdio.h>
#include<string.h>
#define N 100002
int n, m, x, y, c, tata[N], i, j, k, w, nivel[N];
void citire();
int main(){
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
citire();
return 0;
}
void citire(){
scanf("%d %d", &n, &m);
memset(nivel, 1, (n+1)* sizeof(int));
for (; m; m--){
scanf("%d %d %d", &c, &x, &y);
if (c == 1){
for (i = x; tata[i] != 0; i = tata[i]);
for (j = y; tata[j] != 0; j = tata[j]);
if (nivel[i] > nivel[j]) tata[j] = i;
else tata[i] = j;
if (nivel[i] == nivel[j]) nivel[j] ++;
}
else{
for (i = x; tata[i] != 0; i = tata[i]);
for (j = y; tata[j] != 0; j = tata[j]);
if (i == j){
printf("DA\n");
if (tata[x] > 0)
for (k = x; tata[k] != i; k = w){
w = tata[k]; tata[k] = i;
}
if (tata[y] > 0)
for (k = y; tata[k] != i; k = w){
w = tata[k]; tata[k] = i;
}
}
else{
if (tata[x] > 0)
for (k = x; tata[k] != i; k = w){
w = tata[k]; tata[k] = i;
}
if ( tata[y] > 0 )
for (k = y; tata[k] != j; k = w){
w = tata[k]; tata[k] = j;
}
printf("NU\n");
}
}
}
}