Pagini recente » Cod sursa (job #2967032) | Cod sursa (job #1642985) | Cod sursa (job #2070656) | Cod sursa (job #446828) | Cod sursa (job #3189932)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, q;
vector<int> T;
int cerinta, nodeX, nodeY;
int getRoot(int node) {
while (T[node] > 0) {
node = T[node];
}
return node;
}
void join(int nodeA, int nodeB) {
int rootA = getRoot(nodeA);
int rootB = getRoot(nodeB);
if (rootA == rootB) {
return;
}
if (T[rootA] <= T[rootB]) {
T[rootA] += T[rootB];
T[rootB] = rootA;
} else {
T[rootB] += T[rootA];
T[rootA] = rootB;
}
}
bool query(int nodeA, int nodeB) {
return getRoot(nodeA) == getRoot(nodeB);
}
int main() {
fin >> n >> q;
T.assign(n + 1, -1);
while (q--) {
fin >> cerinta >> nodeX >> nodeY;
if (cerinta == 1) {
join(nodeX, nodeY);
} else {
bool sameForest = query(nodeX, nodeY);
if (sameForest) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}