Pagini recente » Cod sursa (job #1638859) | Cod sursa (job #2115160) | Cod sursa (job #2434257) | Cod sursa (job #2838389) | Cod sursa (job #1105420)
#include <cstdio>
#define NMAX 100001
int depth[NMAX];
int father[NMAX];
int n,m;
int root(int node){
while(father[node])
node = father[node];
return node;
}
void compose(int x,int y){
x = root(x);
y = root(y);
if(depth[x] > depth[y])
father[y] = x;
else if(depth[x] < depth[y])
father[x] = y;
else {
father[x] = y;
++depth[y];
}
}
int main(){
int x,y,code;
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
while(m){
scanf("%d%d%d",&code,&x,&y);
if(code == 1)
compose(x,y);
else{
if(root(x) == root(y))
printf("DA\n");
else printf("NU\n");
}
--m;
}
return 0;
}