Pagini recente » Cod sursa (job #786876) | Cod sursa (job #1941366) | Cod sursa (job #690740) | Cod sursa (job #2009156) | Cod sursa (job #2054402)
#include <cstdio>
using namespace std;
const int NMAX = 100000;
int t[NMAX+1],h[NMAX+1];
int GetRootSet(int x){
while(t[x]!=x){
x=t[x];
}
return x;
}
void UniteSet(int x,int y){
if (h[x]==h[y]){
t[y]=x;
h[x]++;
}
if (h[x]>h[y]){
t[y]=x;
}else{
t[x]=y;
}
}
int main() {
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m;
int cod,x,y;
scanf("%d%d",&n,&m);
for (int i=0;i<=n;i++){
t[i]=i;
h[i]=1;
}
for (int i=0;i<m;i++){
scanf("%d%d%d",&cod,&x,&y);
if (cod==1){
x=GetRootSet(x);
y=GetRootSet(y);
UniteSet(x,y);
}else{
if (GetRootSet(x)==GetRootSet(y)){
printf("DA\n");
}else{
printf("NU\n");
}
}
}
return 0;
}