Pagini recente » Cod sursa (job #2593877) | Cod sursa (job #783306) | Cod sursa (job #722462) | Cod sursa (job #2164783) | Cod sursa (job #2942734)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int> tata;
int radacina(int nod) {
if(tata[nod] == 0)
return nod;
tata[nod] = radacina(tata[nod]);
return tata[nod];
}
int main() {
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, op, x, y;
fin >> n >> m;
tata.resize(n+1);
vector<int> inaltime(n+1);
for (int i = 1; i <= m; i++) {
fin >> op >> x >> y;
if (op == 1) {
int tx = radacina(x);
int ty = radacina(y);
int hx = inaltime[tx];
int hy = inaltime[ty];
if (hx > hy) {
tata[ty] = tx;
}
else if(hx < hy) {
tata[tx] = ty;
}
else {
tata[ty] = tx;
inaltime[tx]++;
}
}
else if (op == 2) {
if (radacina(x) == radacina(y))
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
return 0;
}