Pagini recente » Cod sursa (job #1894686) | Cod sursa (job #2932231) | Cod sursa (job #2404949) | Cod sursa (job #2145430) | Cod sursa (job #2809826)
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int parent[100005];
int depth[100005];
int n, m;
int FindRoot(int node)
{
if (node == parent[node])
{
return node;
}
return FindRoot(parent[node]);
}
void SetUnion(int x, int y)
{
int rootX = FindRoot(x);
int rootY = FindRoot(y);
if (depth[rootX] > depth[rootY])
{
// rootX
parent[rootY] = rootX;
}
else
{
// rootY
if (depth[rootX] == depth[rootY])
{
depth[rootX]++;
}
parent[rootX] = rootY;
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i < n; i++)
{
parent[i] = i;
}
while (m--)
{
int t, x, y;
fin >> t >> x >> y;
if (t == 1)
{
SetUnion(x, y);
}
else
{
if (FindRoot(x) == FindRoot(y))
{
fout << "DA" << '\n';
}
else
{
fout << "NU" << '\n';
}
}
}
}