Cod sursa(job #834217)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
ifstream input("disjoint.in");
ofstream output("disjoint.out");
int N, M;
input >> N >> M;
int* parent = new int[N + 1];
for (int i = 1; i <= N; i++) {
parent[i] = 0;
}
for (int i = 0; i < M; i++) {
int instruction;
int x, y;
input >> instruction >> x >> y;
// get x root
int x_root = x;
while (parent[x_root] != 0)
x_root = parent[x_root];
// get y root
int y_root = y;
while (parent[y_root] != 0)
y_root = parent[y_root];
// join
if (instruction == 1) {
parent[y_root] = x_root;
} else if (x_root == y_root) {
output << "DA\n";
} else {
output << "NU\n";
}
}
}