Pagini recente » Cod sursa (job #3185439) | Cod sursa (job #110804) | Cod sursa (job #1006657) | Cod sursa (job #2738295) | Cod sursa (job #2408819)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
void print(const vector<int>& tati) {
for (int tata : tati) {
cout << tata << ' ';
}
cout << '\n';
}
int find_father(const vector<int>& tati, int nod) {
if (tati[nod] == nod) {
return nod;
} else {
return find_father(tati, tati[nod]);
}
}
int main() {
ifstream in("disjoint.in");
int n;
in >> n;
vector<int> tati(n);
iota(tati.begin(), tati.end(), 0);
ofstream out("disjoint.out");
int m;
in >> m;
for (int i = 0; i < m; ++i) {
int operatie, x, y;
in >> operatie >> x >> y;
--x;
--y;
//print(tati);
if (operatie == 1) {
tati[x] = y;
} else {
int tata_x = find_father(tati, x);
int tata_y = find_father(tati, y);
out << (tata_x == tata_y ? "DA" : "NU") << '\n';
}
}
}