Pagini recente » Cod sursa (job #2200043) | Cod sursa (job #2676479) | Cod sursa (job #2584614) | Cod sursa (job #1902674) | Cod sursa (job #1799204)
#include <cstdio>
using namespace std;
int t[100005], h[100005];
int FindSet(int x){
while (t[x] != x)
x = t[x];
return x;
}
void UnionSet(int x, int y){
if (h[x] == h[y]){
h[x] ++;
t[y] = x;
}
else if (h[x] > h[y])
t[y] = x;
else
t[x] = y;
}
int main(){
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n, m, i, x, y, op;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i ++){
h[i] = 1;
t[i] = i;
}
for (i = 1; i <= m; i ++){
scanf("%d%d%d", &op, &x, &y);
if (op == 1)
UnionSet(FindSet(x), FindSet(y));
else{
if (FindSet(x) == FindSet(y))
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}