Pagini recente » Cod sursa (job #223068) | Cod sursa (job #1035846) | Cod sursa (job #314720) | Cod sursa (job #356671) | Cod sursa (job #2940984)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector<int> root;
int getroot(int x)
{
while (root[x] != x)
{
root[x] = root[root[x]];
x = root[x];
}
return x;
}
void union1(int x, int y)
{
int p = getroot(x);
int q = getroot(y);
root[q] = root[p];
}
int main()
{
int n, m, cod, x, y;
f >> n >> m;
vector<vector<int>> adjacence_list(n+1, vector<int>(0));
root = vector<int>(n + 1);
for (int i = 1; i <= n; i++)
{
adjacence_list[i].push_back(i);
root[i] = i;
}
for (int i = 1; i <= m; i++)
{
f >> cod >> x >> y;
if (cod == 1) {
union1(x,y);
}
else
{
if (getroot(root[x]) == getroot(root[y]))
g << "DA\n";
else
g << "NU\n";
}
}
return 0;
}