Pagini recente » Cod sursa (job #413112) | Cod sursa (job #1841262) | Cod sursa (job #1873765) | Cod sursa (job #2656929) | Cod sursa (job #1508332)
#include<stdio.h>
#include<algorithm>
using namespace std;
int tata[101010],tip,a,b,h[101010],N,M;
int findx(int x) {
int R = x, y;
while(tata[R] != R)
R = tata[R];
while(tata[x]!=x) {
y = tata[x];
tata[x] = R;
x = y;
}
return R;
}
void unite(int x,int y) {
x = findx(x);
y = findx(y);
if (x == y) return;
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(findx(a),findx(b));
}
if(tip==2)
{
if(findx(a)==findx(b))
printf("DA\n");
else printf("NU\n");
}
}
}