Pagini recente » Cod sursa (job #749861) | Cod sursa (job #813769) | Cod sursa (job #1681158) | Cod sursa (job #347763) | Cod sursa (job #2940983)
#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];
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] = new int(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;
}