Pagini recente » Cod sursa (job #306675) | Cod sursa (job #2879632) | Cod sursa (job #2054514) | Monitorul de evaluare | Cod sursa (job #3300331)
#include <bits/stdc++.h>
using namespace std;
int tata[100005];
int height[100005];
int getRoot(int node) {
int root = node;
while (root != tata[root]) {
root = tata[root];
}
while (node != root) {
int temp = tata[node];
tata[node] = root;
node = temp;
}
return root;
}
void unite(int x, int y) {
int rootX = getRoot(x);
int rootY = getRoot(y);
if (height[rootX] < height[rootY]) {
tata[rootX] = rootY;
} else if (height[rootY] < height[rootX]) {
tata[rootY] = rootX;
} else {
tata[rootX] = rootY;
height[rootY] += 1;
}
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
tata[i] = i;
height[i] = 1;
}
for (int i = 1; i <= m; ++i) {
int type, x, y;
cin >> type >> x >> y;
if (type == 1) {
unite(x, y);
} else {
if (getRoot(x) == getRoot(y)) {
cout << "DA\n";
} else {
cout << "NU\n";
}
}
}
return 0;
}