Pagini recente » Cod sursa (job #2751024) | Cod sursa (job #2977574) | Cod sursa (job #2111838) | Cod sursa (job #1538124) | Cod sursa (job #1148596)
#include <cstdio>
#define NMAX 100005
using namespace std;
int n, m;
int t[NMAX], h[NMAX];
void join(int, int);
void query(int, int);
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int cod, x, y;
for (int i = 1; i <= n; ++i) h[i] = 1;
scanf("%d %d", &n, &m);
while (m--)
{
scanf("%d %d %d", &cod, &x, &y);
if (cod < 2) join(x, y);
else query(x, y);
}
return 0;
}
void join(int x, int y)
{
while (t[x]) x = t[x];
while (t[y]) y = t[y];
if (h[x] > h[y])
{
t[y] = x;
h[x] += h[y];
//h[y] = 0;
}
else
{
t[x] = y;
h[y] += h[x];
//h[x] = 0;
}
}
void query(int x, int y)
{
int a = x, b = y;
int aux;
while (t[a]) a = t[a];
while (t[b]) b = t[b];
if (a == b) printf("DA\n");
else printf("NU\n");
while (t[x] != a && t[x])
{
aux = x;
x = t[x];
t[aux] = a;
}
while (t[y] != b && t[y])
{
aux = y;
y = t[y];
t[aux] = b;
}
}