Pagini recente » Cod sursa (job #194086) | Cod sursa (job #599287) | Cod sursa (job #1364675) | Cod sursa (job #1950525) | Cod sursa (job #588882)
Cod sursa(job #588882)
#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)
{
if(h[x]>h[y])
{
tata[y]=x;
}
else tata[x]=y;
if(h[x]==h[y])
h[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;
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");
}
}
}