Pagini recente » Cod sursa (job #763668) | Cod sursa (job #3177712) | Cod sursa (job #310569) | Cod sursa (job #2967876) | Cod sursa (job #2945748)
#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;
unsigned int getDoD(unsigned int currNode) {
if (currNode != dads[currNode]) {
return getDoD(dads[currNode]);
}
return currNode;
}
int main() {
fin >> n >> m;
dads.resize(n + 1);
for (unsigned int i = 1; 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[getDoD(operands.second)] = dads[getDoD(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;
}