Pagini recente » Cod sursa (job #3136213) | Cod sursa (job #1124521) | Cod sursa (job #2075905) | Cod sursa (job #443503) | Cod sursa (job #3287015)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int parent[MAX];
void Union(int x, int y)
{
parent[y] = x;
}
int FindRoot(int x)
{
int rad = x;
while (parent[rad] != 0)
rad = parent[rad];
while (x != rad)
{
int y = parent[x];
parent[x] = rad;
x = y;
}
return rad;
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int op, x, y;
cin >> op >> x >> y;
x = FindRoot(x);
y = FindRoot(y);
if (op == 1)
{
if (x != y)
Union(x, y);
}
else
{
if (x == y)
cout << "DA\n";
else
cout << "NU\n";
}
}
return 0;
}