Pagini recente » Cod sursa (job #2055120) | Cod sursa (job #3141846) | Cod sursa (job #2354968) | Cod sursa (job #1585818) | Cod sursa (job #3138628)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
const int nmax = 1e5 + 9;
int root[nmax], sz[nmax];
int find(int x) {
if(root[x] == x) {
return x;
}
root[x] = find(root[x]);
return root[x];
}
void comb(int a, int b) {
if(sz[a] < sz[b]) {
swap(a, b);
}
sz[a] += sz[b];
root[a] = b;
}
int main() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
root[i] = i, sz[i] = 1;
}
for(int i = 1; i <= m; i ++) {
int op, a, b;
cin >> op >> a >> b;
if(op == 1) {
comb(find(a), find(b));
} else {
if(find(a) == find(b)) {
cout << "DA\n";
} else {
cout << "NU\n";
}
}
}
return 0;
}