Pagini recente » Cod sursa (job #2460330) | Cod sursa (job #557226) | Cod sursa (job #2431642) | Cod sursa (job #995552) | Cod sursa (job #2424907)
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int main()
{
int n, m;
in>>n;
vector<int> comp(n+1);
in>>m;
vector<vector<int>> nod_comp(n+1);
for(int i = 0; i < n; i += 1) {
comp[i] = i;
nod_comp[i].push_back(i);
}
int cod, x, y;
for(int i=0;i<m;i++)
{
in>>cod>>x>>y;
x--, y--;
nod_comp[x].push_back(x);
nod_comp[y].push_back(y);
if(cod == 1)
{
if(nod_comp[comp[x]].size() < nod_comp[comp[y]].size())
{
for (auto j : nod_comp[x])
{
comp[j] = y;
nod_comp[y].push_back(x);
}
nod_comp[x].clear();
}
else
{
for (auto j : nod_comp[y])
{
comp[j] = x;
nod_comp[x].push_back(y);
}
nod_comp[y].clear();
}
}
else if (cod == 2)
{
if (comp[x] == comp[y])
out << "DA\n";
else
out << "NU\n";
}
}
return 0;
}