Pagini recente » Cod sursa (job #744240) | Cod sursa (job #2572911) | Cod sursa (job #2202032) | Cod sursa (job #2713479) | Cod sursa (job #2772176)
#include <fstream>
std::ifstream fin ("disjoint.in");
std::ofstream fout ("disjoint.out");
int const nmax = 100005;
int parent[nmax];
int size[nmax];
int root (int x) {
if (parent[x] == 0)
return x;
else
parent[x] = root(parent[x]);
return parent[x];
}
void unite (int x, int y) {
x = root(x);
y = root(y);
if (size[x] > size[y]) {
parent[y] = x;
size[x] += size[y];
size[y] = 0;
} else {
parent[x] = y;
size[y] += size[x];
size[x] = 0;
}
}
int check (int x, int y) {
return (root(x) == root(y));
}
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= n; i++) {
parent[i] = 0;
size[i] = 1;
}
for (int i = 1; i <= m; i++) {
int cod, x, y;
fin >> cod >> x >> y;
if (cod == 1)
unite (x, y);
else {
if (check(x, y) == 1)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}