Pagini recente » Cod sursa (job #3226227) | Cod sursa (job #2345796) | Cod sursa (job #1579530) | Borderou de evaluare (job #702984) | Cod sursa (job #863284)
Cod sursa(job #863284)
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxN 100005
int tata[maxN] , H[maxN];
int root (int x)
{
int R;
for (R = x ; R != tata[R] ; R = tata[R]);
for (int y = x ; y != R ; tata[y] = R , y = tata[y]);
return R;
}
void update (int x , int y)
{
int root_x = root (x);
int root_y = root (y);
if (H[root_x] > H[root_y])
tata[root_y] = root_x;
else if (H[root_x] < H[root_y])
tata[root_x] = root_y;
else
{
++H[root_x];
tata[root_y] = root_x;
}
}
int main ()
{
freopen ("disjoint.in" , "r" , stdin);
freopen ("disjoint.out" , "w" , stdout);
int N , M;
scanf ("%d %d" , &N , &M);
for (int i = 1 ; i <= N ; ++i)
tata[i] = i;
int x , y , cod;
while (M --)
{
scanf ("%d %d %d" , &cod , &x , &y);
if (cod == 1)
update (x , y);
else
{
if (root (x) == root (y))
printf ("DA\n");
else printf ("NU\n");
}
}
return 0;
}