Pagini recente » Cod sursa (job #2084149) | Cod sursa (job #2432366) | Cod sursa (job #1425755) | Cod sursa (job #1974919) | Cod sursa (job #2941305)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100005;
int n, m;
int tata[NMAX], rang[NMAX];
int radacina(int x) {
while (tata[x] != x) {
x = tata[x];
}
return x;
}
void reuniune(int x, int y) {
if (rang[x] < rang[y])
tata[x] = y;
else if (rang[x] > rang[y])
tata[y] = x;
else {
tata[x] = y;
rang[y]++;
}
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; i++) {
tata[i] = i;
rang[i] = 1;
}
for (int i = 0; i < m; i++) {
int cod, x, y;
fin >> cod >> x >> y;
x = radacina(x);
y = radacina(y);
if (cod == 1) {
reuniune(x, y);
}
else {
if (x == y)
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
}