Pagini recente » Cod sursa (job #399387) | Cod sursa (job #260833) | Cod sursa (job #2259887) | Cod sursa (job #401927) | Cod sursa (job #3175298)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define NMAX 100005
int tata[NMAX], rang[NMAX];
int radacina(int x) {
int parinte = x;
while (parinte != tata[parinte]) {
parinte = tata[parinte];
}
while (x != tata[x]) {
int y = tata[x];
tata[x] = parinte;
x = y;
}
return parinte;
}
void uneste(int x, int y) {
if (rang[x] < rang[y]) {
tata[x] = y;
} else {
tata[y] = x;
if (rang[x] == rang[y]) {
rang[y]++;
}
}
}
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
tata[i] = i;
rang[i] = 1;
}
while (m--) {
int cod, x, y;
fin >> cod >> x >> y;
if (cod == 1) {
uneste(radacina(x), radacina(y));
} else {
if (radacina(x) == radacina(y)) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}