Pagini recente » Cod sursa (job #1858118) | Cod sursa (job #1062551) | Cod sursa (job #1689460) | Cod sursa (job #2920230) | Cod sursa (job #235900)
Cod sursa(job #235900)
#include <cstdio>
#include <cstring>
using namespace std;
#define FIN "disjoint.in"
#define FOUT "disjoint.out"
#define MAX_N 100005
int M[MAX_N];
int H[MAX_N];
int N, K;
int tata (int c)
{
while (M[c] != c)
c = M[c];
return c;
}
int main ()
{
int cod, x, y, i;
freopen (FIN, "r", stdin);
freopen (FOUT, "w", stdout);
scanf ("%d %d", &N, &K);
for (i = 1; i <= N; ++i) M[i] = i, H[i] = 1;
while (K--)
{
scanf ("%d %d %d", &cod, &x, &y);
if (cod == 1)
{
if (H[x] < H[y])
{
int tx = tata(x), ty = tata (y);
M[tx] = ty;
}
if (H[x] > H[y])
{
int tx = tata(x), ty = tata (y);
M[ty] = tx;
}
if (H[x] == H[y])
{
int tx = tata(x), ty = tata (y);
M[tx] = ty, ++H[ty];
}
}
else if (tata(x) == tata(y)) printf ("DA\n"); else printf ("NU\n");
}
return 0;
}