Pagini recente » Cod sursa (job #671326) | Cod sursa (job #1141479) | Cod sursa (job #2569137) | Cod sursa (job #2866630) | Cod sursa (job #2028653)
#include <iostream>
#include <stdio.h>
#include <vector>
#define NMax 100005
using namespace std;
int tata[NMax],rang[NMax],N,M,X,Y,op;
int Radacina(int x)
{
int aux,rad;
for(rad=x;rad!=tata[rad];rad=tata[rad]);
while(tata[x]!=x)
{
aux=tata[x];
tata[x]=rad;
x=aux;
}
return rad;
}
void Union(int x,int y)
{
if(rang[x]>rang[y])
tata[y]=x;
else tata[x]=y;
if(rang[y]==rang[x])
rang[y]++;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
{
tata[i]=i;
rang[i]=1;
}
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&op,&X,&Y);
if(op==1)
{
Union(Radacina(X),Radacina(Y));
}
else if(op==2)
{
if(Radacina(Y)==Radacina(X))
printf("DA\n");
else printf("NU\n");
}
}
}