Pagini recente » Cod sursa (job #2977411) | Cod sursa (job #2125063) | Cod sursa (job #2700626) | Cod sursa (job #2179893) | Cod sursa (job #2376528)
#include <fstream>
#define NMAX 100010
std::ifstream fin("disjoint.in");
std::ofstream fout("disjoint.out");
int n, m;
int father[NMAX];
int height[NMAX];
int find(int x) {
int root = x;
while (father[root])
root = father[root];
int aux;
while (father[x]) {
aux = father[x];
father[x] = root;
x = aux;
}
return root;
}
void unite(int rootX, int rootY) {
if (height[rootX] < height[rootY])
father[rootX] = rootY;
else {
father[rootY] = rootX;
if (height[rootX] == height[rootY])
height[rootX]++;
}
}
int main() {
fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, z;
fin >> x >> y >> z;
if (x == 1)
unite(find(y), find(z));
else
fout << (find(y) == find(z) ? "DA\n" : "NU\n");
}
fout.close();
return 0;
}