Pagini recente » Cod sursa (job #2105632) | Cod sursa (job #1225620) | Cod sursa (job #2574000) | Cod sursa (job #2240658) | Cod sursa (job #1896235)
#include <cstdio>
#include <stack>
using namespace std;
struct node{
int rank;
int parent;
}node[100001];
int Find(int target){
int root, y;
for(root = target; node[root].parent != root; root = node[root].parent);
for(; node[target].parent != target;){
y = node[target].parent;
node[target].parent = root;
target = y;
}
return root;
}
void Union(int x, int y){
int rootX = Find(x);
int rootY = Find(y);
if(node[rootX].rank > node[rootY].rank){
node[rootY].parent = rootX;
}else{
if(node[rootX].rank == node[rootY].rank){
node[rootY].rank++;
}
node[rootX].parent = rootY;
}
}
int main(){
int sets, queries, task, x, y;
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &sets, &queries);
for(int i = 1; i <= sets; i++){
node[i].rank = 1;
node[i].parent = i;
}
for(int i = 1; i <= queries; i++){
scanf("%d %d %d", &task, &x, &y);
if(task == 1){
Union(x, y);
}else{
printf("%s\n", (Find(x) == Find(y)) ? "DA" : "NU");
}
}
return 0;
}