Pagini recente » Cod sursa (job #854522) | Cod sursa (job #1587148) | Cod sursa (job #1028425) | Cod sursa (job #1009195) | Cod sursa (job #2423580)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100000;
int cer, x, y;
int N, M;
int arb[NMAX];
int Find(int x){
int y, aux;
while(arb[x] != x)
x = arb[x];
y = x;
while(x != y){
aux = arb[x];
arb[x] = y;
x = aux;
}
return y;
}
void Unite(int x,int y){
y = Find(y);
x = Find(x);
arb[y] = x;
}
int main(){
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i++)
arb[i] = i;
for(int k = 0; k < M; k++){
scanf("%d%d%d", &cer, &x, &y);
if(cer == 1){
Unite(x, y);
} else {
if(Find(arb[x]) == Find(arb[y]))
printf("DA\n");
else
printf("NU\n");
}
}
//for(int i = 1; i <= N; i++)
//printf("%d ", Find(arb[i]));
return 0;
}