Pagini recente » Cod sursa (job #1714998) | Cod sursa (job #960096) | Cod sursa (job #229834) | Cod sursa (job #107316) | Cod sursa (job #2203609)
// https://goo.gl/fBmFxu
#include <bits/stdc++.h>
using namespace std;
#define NMAX 100009
#define MMAX 200009
#define kInf (1 << 30)
#define kInfLL (1LL << 60)
#define kMod 666013
#define edge pair<int, int>
#define x first
#define y second
#define USE_FILES "MLC"
#ifdef USE_FILES
#define cin fin
#define cout fout
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#endif
// number of tests from "in"
int test_cnt = 1;
void clean_test();
// your global variables are here
int N, Q, type, x, y, dad[NMAX], rang[NMAX];
int findDad(int node) {
if (dad[node] != node) {
dad[node] = findDad(dad[node]);
}
return dad[node];
}
void unite(int x, int y) {
int px = findDad(x);
int py = findDad(y);
if (rang[px] < rang[py]) {
dad[px] = py;
rang[px] += rang[py];
} else {
dad[py] = px;
rang[py] += rang[px];
}
}
// your solution is here
void solve() {
cin >> N >> Q;
for (int i = 1; i <= N; ++i) {
dad[i] = i;
rang[i] = 1;
}
while (Q--) {
cin >> type >> x >> y;
if (type == 1) {
unite(x, y);
} else {
if (findDad(x) == findDad(y)) {
cout << "DA\n";
} else {
cout << "NU\n";
}
}
}
if (test_cnt > 0) {
clean_test();
}
}
void clean_test() {
// clean if needed
}
int main() {
// cin >> test_cnt;
while (test_cnt--) {
solve();
}
return 0;
}