Pagini recente » Cod sursa (job #373176) | Cod sursa (job #2774770) | Cod sursa (job #1449900) | Cod sursa (job #2672042) | Cod sursa (job #2170514)
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAXN = 100005;
int n, m;
int v[MAXN], r[MAXN];
int find(int k) {
if (v[k] != k) {
v[k] = find(v[k]);
}
return v[k];
}
void unite(int x, int y) {
int px = find(x);
int py = find(y);
if (r[px] > r[py]) {
v[py] = px;
}
else if (r[px] < r[py]) {
v[px] = py;
}
else {
v[px] = v[py];
r[py]++;
}
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
v[i] = i;
r[i] = 1;
}
for (int i = 1; i <= m; ++i) {
int q, x, y;
fin >> q >> x >> y;
if (q == 1) {
unite(x, y);
}
else {
if (find(x) == find(y)) {
fout << "DA";
}
else {
fout << "NU";
}
fout << '\n';
}
}
fout.close();
return 0;
}