Pagini recente » Cod sursa (job #3290429) | Cod sursa (job #281019) | Cod sursa (job #3195466) | Cod sursa (job #3274808) | Cod sursa (job #1915772)
#include <cstdio>
using namespace std;
struct node{
int rank : 17;
int parent : 17;
}node[100001];
int Find(int target){
int root = target, oldParent;
while(node[root].parent != root){
root = node[root].parent;
}
while(node[target].parent != target){
oldParent = node[target].parent;
node[target].parent = root;
target = oldParent;
}
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;
}