Pagini recente » Cod sursa (job #2907286) | Cod sursa (job #1977443) | Cod sursa (job #1614858) | Cod sursa (job #2714726) | Cod sursa (job #2916547)
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
struct DSU {
vector<int> e;
DSU(int N) { e = vector<int>(N, -1); }
// get representive component (uses path compression)
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
}
};
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
fin >> n >> m;
DSU dsu(n);
while (m--) {
int ty, a, b;
fin >> ty >> a >> b;
--a, --b;
if (ty == 1) {
dsu.unite(a, b);
} else {
fout << (dsu.same_set(a, b) ? "DA" : "NU") << nl;
}
}
}