Pagini recente » Cod sursa (job #2042916) | Cod sursa (job #1340519) | Cod sursa (job #1254363) | Cod sursa (job #1632351) | Cod sursa (job #1606369)
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define DIM 100005
int tata[DIM], h[DIM];
int N, M, tip, x, y;
void Union(int x, int y);
int Find(int x);
int main() {
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d %d\n", &N, &M);
for(int i = 1; i <= M; ++i) {
scanf("%d %d %d\n", &tip, &x, &y);
if(tip == 1) {
Union(x, y);
}
else {
int rx = Find(x);
int ry = Find(y);
if(rx == ry) {
cout << "DA\n";
}
else {
cout << "NU\n";
}
}
}
return 0;
}
void Union(int x, int y) {
int rx = Find(x);
int ry = Find(y);
if(h[rx] > h[ry]) {
tata[ry] = rx;
}
else {
tata[rx] = ry;
}
if(h[rx] == h[ry]) {
h[ry]++;
}
}
int Find(int x) {
int rx = x;
while(tata[rx]) {
rx = tata[rx];
}
while(x != rx) {
int y = tata[x];
tata[x] = rx;
x = y;
}
return rx;
}