Pagini recente » Cod sursa (job #135085) | Cod sursa (job #2175596) | Cod sursa (job #10951) | Cod sursa (job #628261) | Cod sursa (job #1464517)
#include<cstdio>
using namespace std;
int h[100010],dad[100010];
int find_dad(int nod){
if(dad[nod]==nod)
return nod;
int x=find_dad(dad[nod]);
dad[nod]=x;
return x;
}
void query(int x,int y){
if(find_dad(x)==find_dad(y))
printf("DA\n");
else
printf("NU\n");
}
void join(int x,int y){
int dad1=find_dad(x),dad2=find_dad(y);
if(h[dad1]>h[dad2])
dad[dad2]=dad1;
else
if(h[dad2]>h[dad1])
dad[dad1]=dad2;
else{
dad[dad1]=dad2;
h[dad2]++;
}
}
int main(){
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int n,m,i,cod,x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
dad[i]=i;
for(i=1;i<=m;i++){
scanf("%d%d%d",&cod,&x,&y);
if(cod==1)
join(x,y);
else
query(x,y);
}
return 0;
}