Pagini recente » Cod sursa (job #335789) | Cod sursa (job #37928) | Cod sursa (job #70546) | Cod sursa (job #1045854) | Cod sursa (job #1238375)
#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;
}