Pagini recente » Cod sursa (job #316547) | Cod sursa (job #728991) | Cod sursa (job #1102404) | Cod sursa (job #877220) | Cod sursa (job #3287016)
#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()
{
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
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;
}