Pagini recente » Cod sursa (job #1759937) | Cod sursa (job #3154364) | Cod sursa (job #1943987) | Cod sursa (job #797187) | Cod sursa (job #1238277)
#include <cstdio>
#define NMAX 100000
int n,m;
int father[NMAX];
int height[NMAX];
int root(int node){
while(father[node])
node = father[node];
return node;
}
void unite(int node1, int node2){
node1 = root(node1);
node2 = root(node2);
if(height[node1] > height[node2])
father[node2] = node1;
else if(height[node1] == height[node2])
father[node1] = node2;
else { father[node1] = node2;
height[node2]++;
}
}
void solve(){
int op,x,y;
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
for(register int i=1;i<=m;++i){
scanf("%d%d%d",&op,&x,&y);
if(op == 1)
unite(x,y);
else{
if(root(x) == root(y))
printf("DA\n");
else printf("NU\n");
}
}
}
int main(){
solve();
return 0;
}