Pagini recente » Cod sursa (job #427995) | Cod sursa (job #2971737) | Cod sursa (job #4625) | Cod sursa (job #1548327) | Cod sursa (job #3269670)
#include <unordered_map>
using namespace std;
#include <iostream>
/* #include <fstream> */
/* ifstream cin("disjoint.in"); */
/* ofstream cout("disjoint.out"); */
int main() {
int n, q;
cin >> n >> q;
unordered_map<int, int> parents;
for (int i = 1; i <= n; i++) {
parents[i] = i;
}
for (int _ = 0; _ < q; _++) {
int t, a, b;
cin >> t >> a >> b;
int rootA = a, rootB = b;
while (parents[rootA] != rootA) {
rootA = parents[rootA];
}
while (parents[rootB] != rootB) {
rootB = parents[rootB];
}
if (t == 1) {
parents[rootA] = rootB;
} else {
cout << (rootA == rootB ? "DA\n" : "NU\n");
}
}
return 0;
}