Pagini recente » Cod sursa (job #1659925) | Cod sursa (job #2815259) | Cod sursa (job #2039377) | Cod sursa (job #2860552) | Cod sursa (job #342815)
Cod sursa(job #342815)
#include <stdio.h>
#define DIM 100005
int p[DIM],r[DIM];
int n,m;
void init ()
{
int i;
scanf ("%d%d",&n,&m);
for (i=1; i<=n; ++i)
p[i]=i;
}
void unite (int x,int y)
{
if (r[x]<r[y])
p[x]=y;
else
p[y]=x;
if (r[x]==r[y])
++r[x];
}
int find (int x)
{
if (x!=p[x])
p[x]=find (p[x]);
return p[x];
}
void solve ()
{
int i,tip,x,y;
for (i=1; i<=m; ++i)
{
scanf ("%d%d%d",&tip,&x,&y);
if (tip==1)
unite (find (x),find (y));
else
if (find (x)==find (y))
printf ("DA\n");
else
printf ("NU\n");
}
}
int main ()
{
freopen ("disjoint.in","r",stdin);
freopen ("disjoint.out","w",stdout);
init ();
solve ();
return 0;
}