Pagini recente » Cod sursa (job #2070654) | Cod sursa (job #3241215) | Cod sursa (job #35018) | Cod sursa (job #1549985) | Cod sursa (job #1906013)
#include <cstdio>
using namespace std;
const int nmax=100000;
int t[nmax+5];
int h[nmax+5];
int findMD(int x){
while (x!=t[x]){
x=t[x];
}
return x;
}
void unionMD(int tx,int ty){
if (h[tx]==h[ty]){
h[tx]++;
t[ty]=tx;
}else{
if (h[tx]>h[ty]){
t[ty]=tx;
}else{
t[tx]=ty;
}
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for (int test=1;test<=m;test++){
int cod,x,y;
scanf("%d%d%d",&cod,&x,&y);
if (cod==1){
int tx=findMD(x);
int ty=findMD(y);
if (tx!=ty){
unionMD(tx,ty);
}
}else{
if (cod==2){
int tx=findMD(x);
int ty=findMD(y);
if (tx==ty){
printf("DA\n");
}else{
printf("NU\n");
}
}
}
}
return 0;
}