Pagini recente » Cod sursa (job #14878) | Cod sursa (job #879547) | Cod sursa (job #2144679) | Cod sursa (job #2927476) | Cod sursa (job #2946415)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector<int> root;
int findRoot(int x)
{
if (root[x] == 0)
return x;
return root[x] = findRoot(root[x]);
}
int main()
{
int n, m, cod, x, y;
fin >> n >> m;
root.resize(n + 1, 0);
for (int i = 0; i < m; i++)
{
fin >> cod >> x >> y;
if (cod == 1)
{
int root_x = findRoot(x);
int root_y = findRoot(y);
root[root_x] = root_y;
}
else
{
if (findRoot(x) == findRoot(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}