Pagini recente » Cod sursa (job #2372698) | Cod sursa (job #1706146) | Cod sursa (job #551708) | Cod sursa (job #2933927) | Cod sursa (job #1795675)
#include<cstdio>
int sef[100001];
int gaseste(int x)
{
int poz=x;
while(sef[poz])
{
poz=sef[poz];
}
//Compresia caii
int poz1=x;
while(sef[poz1])
{
int c=poz1;
poz1=sef[poz1];
sef[c]=poz;
}
return poz;
}
void uneste(int x,int y)
{
sef[gaseste(x)]=gaseste(y);
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int nr_elem,nr_op;
scanf("%d %d ",&nr_elem,&nr_op);
for(int i=1;i<=nr_op;i++)
{
int tip,x,y;
scanf("%d %d %d ",&tip,&x,&y);
if(tip==1)
{
uneste(x,y);
}
else
if(gaseste(x)==gaseste(y))
printf("DA\n");
else
printf("NU\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}