Cod sursa(job #2390711)
| Utilizator | Data | 28 martie 2019 11:45:54 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 2.04 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, x, y, operatie;
fin >> n >> m;
vector<vector<int>> noduri_comp(n+1);
vector<int> comp(n+1, 0);
int nrComp = 0;
for(int i = 0; i < m; i++)
{
fin >> operatie >> x >> y;
if(operatie == 1)
{
if(comp[x] == 0 & comp[y] == 0)
{
nrComp++;
noduri_comp[nrComp].push_back(x);
noduri_comp[nrComp].push_back(y);
comp[x] = nrComp;
comp[y] = nrComp;
}
else if(comp[x] != comp[y])
{
if(comp[x] != 0 && comp[y] == 0)
{
noduri_comp[comp[x]].push_back(y);
comp[y] = comp[x];
}
else if(comp[y] != 0 && comp[x] == 0)
{
noduri_comp[comp[y]].push_back(x);
comp[x] = comp[y];
}
else if(noduri_comp[comp[x]].size() > noduri_comp[comp[y]].size())
{
for(auto k : noduri_comp[comp[y]])
{
noduri_comp[comp[x]].push_back(k);
comp[k] = comp[x];
}
}
else if(noduri_comp[comp[x]].size() <= noduri_comp[comp[y]].size())
{
for(auto k : noduri_comp[comp[x]])
{
noduri_comp[comp[y]].push_back(k);
comp[k] = comp[y];
}
}
}
}
else
{
if(comp[x] == comp[y])
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}
