Pagini recente » Cod sursa (job #3268820) | Cod sursa (job #3299292) | Cod sursa (job #2691210) | Cod sursa (job #3222985) | Cod sursa (job #3296801)
#include <bits/stdc++.h>
#define NMAX 100000
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, op, x, y;
vector<int> rad(NMAX + 2), sz(NMAX + 2);
int Find(int x) {
if (x == rad[x]) {
return x;
}
return rad[x] = Find(rad[x]);
}
void Union(int x, int y) {
if (sz[x] < sz[y]) {
swap(sz[x], sz[y]);
}
sz[x] += sz[y];
rad[y] = x;
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> n >> m;
for (int i = 1; i <= n; i++) {
rad[i] = i;
sz[i] = 1;
}
while (m--) {
fin >> op >> x >> y;
if (op == 1) {
if (Find(x) != Find(y)) {
Union(Find(x), Find(y));
}
}
else {
fout << (Find(x) == Find(y) ? "DA" : "NU") << '\n';
}
}
return 0;
}