Pagini recente » Cod sursa (job #2722274) | Cod sursa (job #638220) | Cod sursa (job #2104586) | Cod sursa (job #1112590) | Cod sursa (job #1182313)
#include<stdio.h>
#include<algorithm>
using namespace std;
int tata[101010],tip,a,b,h[101010],N,M;
int find(int x)
{
int R,y;
R=x;
while(tata[R]!=R)
R=tata[R];
while(tata[x]!=x)
{
y=tata[x];
tata[x]=R;
x=y;
}
return R;
}
int unite(int x,int y)
{
x=find(x);
y=find(y);
if(h[x]>h[y])
{
tata[y]=x;
h[x]+=h[y];
}
else {
tata[x]=y;
h[y]+=h[x];
}
}
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;
h[i]=1;
}
for(int i=1;i<=M;++i)
{
scanf("%d%d%d",&tip,&a,&b);
if(tip==1)
{
unite(find(a),find(b));
}
if(tip==2)
{
if(find(a)==find(b))
printf("DA\n");
else printf("NU\n");
}
}
}