Pagini recente » Cod sursa (job #469583) | Cod sursa (job #2187298) | Cod sursa (job #1951412) | Cod sursa (job #1195367) | Cod sursa (job #2837326)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.in");
struct deseu {
vector<int> parent;
vector<int> sz;
void build(int n) {
parent = sz = vector<int>(n + 1);
}
void make_set(int x) {
parent[x] = x;
sz[x] = 1;
}
int find_set(int v) {
if (v == parent[v]) {
return v;
}
return parent[v] = find_set(parent[v]);
}
void unite(int a ,int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b]) {
swap(a, b);
}
parent[b] = a;
sz[a] += sz[b];
}
}
};
int main() {
int n,q;
fin >> n >> q;
deseu tree;
tree.build(n + 1);
for (int i = 1; i <= n; ++i) {
tree.make_set(i);
}
while (q--) {
int op;
fin >> op;
if (op == 1) {
int a, b;
fin >> a >> b;
tree.unite(a, b);
}
if (op == 2) {
int a, b;
fin >> a >> b;
fout << (tree.find_set(a) == tree.find_set(b) ? "DA" : "NU") << '\n';
}
}
}