Pagini recente » Cod sursa (job #1284503) | Cod sursa (job #3137259) | Cod sursa (job #209087) | Cod sursa (job #388531) | Cod sursa (job #2945746)
#define ADD_TO_SET 1
#define SHOW_IF_YES 2
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
unsigned int n, m;
vector<unsigned int> dads;
int getDoD(int currNode) {
if (currNode != dads[currNode]) {
return getDoD(dads[currNode]);
}
return currNode;
}
int main() {
fin >> n >> m;
dads.resize(n);
for (unsigned int i = 0; i < n; i++) {
dads[i] = i;
}
unsigned int operation_type; pair<unsigned int, unsigned int> operands;
for (unsigned int i = 0; i < m; i++) {
fin >> operation_type >> operands.first >> operands.second;
switch (operation_type) {
case ADD_TO_SET: {
dads[operands.second] = dads[operands.first];
break;
}
case SHOW_IF_YES: {
if (getDoD(operands.first) == getDoD(operands.second)) {
fout << "DA\n";
}
else fout << "NU\n";
break;
}
default: {
break;
}
}
}
fin.close(); fout.close();
return 0;
}