Pagini recente » Cod sursa (job #2908380) | Cod sursa (job #998783) | Cod sursa (job #3002161) | Cod sursa (job #517209) | Cod sursa (job #3135118)
/*
* Lefter Sergiu - 01/06/2023
*/
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int N = 1e5;
int n, m, t[N + 1], nr[N + 1];
int radacina(int x) {
if (t[x] == 0) {
return x;
}
t[x] = radacina(t[x]);
return t[x];
}
void reuniune(int x, int y) {
int rx = radacina(x);
int ry = radacina(y);
if (nr[rx] < nr[ry]) {
t[rx] = ry;
nr[ry] += nr[rx];
} else {
t[ry] = rx;
nr[rx] += nr[ry];
}
}
bool interogare(int x, int y) {
return (radacina(x) == radacina(y));
}
int main() {
in >> n >> m;
for (int i = 1; i <= n; i++) {
nr[i] = 1;
}
for (int i = 1; i <= m; i++) {
int tip, x, y;
in >> tip >> x >> y;
if (tip == 1) {
reuniune(x, y);
} else {
if (interogare(x, y)) {
out << "DA" << '\n';
} else {
out << "NU" << '\n';
}
}
}
in.close();
out.close();
return 0;
}