Pagini recente » Cod sursa (job #1847240) | Cod sursa (job #3232924) | Cod sursa (job #2654886) | Cod sursa (job #1939382) | Cod sursa (job #300435)
Cod sursa(job #300435)
#include <stdio.h>
#define nm 100010
int i, n, m, j, x, t, y, X, Y;
int r[nm];
int T[nm];
void read()
{
scanf("%d %d ", &n, &m);
for (i=1; i<=n; ++i)
{
T[i] = i;
r[i] = 1;
}
}
int find(int x)
{
int tt, tmp;
for (tt = x; T[tt] != tt; tt = T[tt]);
while (T[x] != x)
{
tmp = T[x];
T[x] = tt;
x = tmp;
}
return tt;
}
void make_union(int x, int y)
{
if (r[x] > r[y])
{
T[y] = x;
}
else
{
T[x] = y;
}
if (r[x] == r[y])
r[y] ++;
}
void solve()
{
for (i=1; i<=m; ++i)
{
scanf("%d %d %d", &t, &x, &y);
if (t == 1)
{
make_union(x, y);
}
else
{
if (find(x) == find(y))
printf("DA\n");
else
printf("NU\n");
}
}
}
void write()
{
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out","w",stdout);
read();
solve();
write();
return 0;
}