Pagini recente » Cod sursa (job #1075579) | Cod sursa (job #198122) | Cod sursa (job #3262087) | Cod sursa (job #2391211) | Cod sursa (job #1831061)
#include<cstdio>
#include<ctime>
#include<cstdlib>
using namespace std;
int dad[200001];
int findd (int nod){
if (nod==dad[nod])
return nod;
else{
nod=findd(dad[nod]);
}
return dad[nod];}
void unionn (int nod1, int nod2){
int a=findd (nod1);
int b=findd (nod2);
if (a!=b){
if (rand()%2==0)
dad[a]=b;
else
dad[b]=a;
}
}
int main()
{
freopen ("disjoint.in","r",stdin);
freopen ("disjoint.out","w",stdout);
int n,m,i,nrmuchii=0,tip,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",&tip,&x,&y);
if (tip==1){ // union
unionn(x,y);
}
if (tip==2){
if (findd(x)==findd(y)){
printf("DA\n");
}
else
printf ("NU\n");
}
}
return 0;
}