Pagini recente » Cod sursa (job #1017195) | Cod sursa (job #1740953) | Cod sursa (job #926787) | Cod sursa (job #994156) | Cod sursa (job #2390709)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int n, m, x, y, operatie;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin >> n >> m;
vector<int> comp(n+1);
vector<vector<int>> compConex(n+1);
for(int i = 1; i <= n; i++)
{
comp[i] = i;
compConex[i].push_back(i);
}
for(int i = 0; i < m; i++)
{
fin >> operatie >> x >> y;
if(operatie == 1)
{
x = comp[x];
y = comp[y];
if(x != y)
{
if(compConex[x].size() < compConex[y].size())
{
for(auto j : compConex[x])
{
comp[j] = y;
compConex[y].push_back(j);
}
compConex[x].clear();
}
else
{
for(auto j : compConex[y])
{
comp[j] = x;
compConex[x].push_back(j);
}
}
}
}
if(operatie == 2){
if(comp[x] != comp[y])
fout << "NU" << "\n";
else
fout << "DA" << "\n";
}
}
fin.close();
fout.close();
return 0;
}