Pagini recente » Cod sursa (job #314196) | Cod sursa (job #44557) | Cod sursa (job #2396661) | Cod sursa (job #2511591) | Cod sursa (job #2935993)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m, cod, x, y;
vector <int> tata;
vector <int> nivel;
int radacina(int nod) {
while (tata[nod])
nod = tata[nod];
return nod;
}
void uneste(int x, int y) {
if (nivel[x] == nivel[y]) {
tata[y] = radacina(x);
++nivel[y];
}
else if (nivel[x] > nivel[y])
tata[y] = radacina(x);
else
tata[x] = radacina(y);
}
int main() {
fin >> n >> m;
tata.resize(n + 1);
nivel.resize(n + 1);
for (int i = 1; i <= m; ++i) {
fin >> cod >> x >> y;
if (cod == 1) {
uneste(x, y);
}
else {
if (radacina(x) == radacina(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}