Pagini recente » Cod sursa (job #3001695) | Cod sursa (job #2192027) | Cod sursa (job #2281555) | Cod sursa (job #297102) | Cod sursa (job #1820479)
#include<fstream>
#include<map>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int father[100001], rankEl[100001];
int findFather(int el) {
if (el == father[el]) {
return el;
}
father[el] = findFather(father[el]);
return father[el];
}
void unionSet(int el1, int el2) {
el1 = findFather(el1);
el2 = findFather(el2);
rankEl[el1] > rankEl[el2] ? father[el2] = el1 : father[el1] = el2;
if (rankEl[el1] == rankEl[el2]) {
rankEl[el2]++;
}
}
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= n; i++) {
father[i] = i; rankEl[i] = 0;
}
for (int i = 1; i <= m; i++) {
int a, b, c;
fin >> a >> b >> c;
if (a == 1) {
unionSet(b, c);
}
else {
if (findFather(b) == findFather(c)) {
fout << "DA\n";
}
else {
fout << "NU\n";
}
}
}
}