Pagini recente » Cod sursa (job #1587728) | Cod sursa (job #2175965) | Cod sursa (job #2714746) | Cod sursa (job #1364896) | Cod sursa (job #1328318)
#include <iostream>
#include <cstdio>
using namespace std;
int parents[100005], n, m;
int getParent(int x)
{
if (parents[x] == x)
return x;
else
{
int alf = getParent(parents[x]);
parents[x] = alf;
return alf;
}
}
void unify(int x, int y)
{
parents[getParent(x)] = getParent(y);
}
int check(int x, int y)
{
return getParent(x) == getParent(y);
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &n, &m);
int x, y, t1;
for (int i = 0; i <= n; i++)
parents[i] = i;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &t1, &x, &y);
if (t1 == 1)
unify(x, y);
else
printf(check(x, y)?"DA\n":"NU\n");
}
return 0;
}