Pagini recente » Cod sursa (job #1361142) | Cod sursa (job #1279565) | Cod sursa (job #804624) | Cod sursa (job #1618122) | Cod sursa (job #1361064)
#include <cstdio>
#include <algorithm>
using namespace std;
#define NR 100002
int n,m,h[NR],r[NR];
int root(int x)
{
int k,r;
r=x;
while(h[r]!=r)
r=h[r];
//compresie:
while(h[x]!=x)
{
k=h[x];
h[x]=r;
x=k;
}
return r;
}
int unire(int x, int y)
{
x=root(x);
y=root(y);
if(r[x]>r[y])
swap(x,y);
h[y]=x;
if(r[x]==r[y])
r[x]++;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int q,x,y;
scanf("%d%d",&n,&m);
for(q=0;q<=n;q++)
{
h[q]=q;
r[q]=1;
}
while(m--)
{
scanf("%d%d%d",&q,&x,&y);
if(q==1)
unire(x,y);
else
{
if(root(x)==root(y))
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}