Dobry den,
potrebovala bych trosku pomoci. Potrebuju zrychlit tento program. Ocenim jakoukoliv radu. Predem Dekuju.
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
void prirad_index(vector<int>& kam, int ktery, int kolik)
{
vector<int>::iterator pom = kam.begin() + ktery - 1;
*pom = kolik;
};
int vrat_index(vector<int>& odkud, int ktery)
{
vector<int>::iterator pom = odkud.begin() + ktery - 1;
return(*pom);
};
void komponenty(int v,
int& index,
vector<bool>& je_ve_stacku,
vector<int>& index_pole,
vector<int>& nejnizsi_cesta,
vector<int>& stack,
vector<vector<int> >& graf )
{
prirad_index(index_pole, v, index);
prirad_index(nejnizsi_cesta, v, index);
++index;
stack.push_back(v);
je_ve_stacku[v - 1] = true;
//for each (v, w) in E do
vector<vector<int> >::iterator v_p = graf.begin() + v - 1;
vector<int>::iterator w = (*v_p).begin();
if ((*v_p).front() != 0)
{
while (w != (*v_p).end())
{
if (vrat_index(index_pole, *w )== -1)
{
//if (w.index is undefined) then
//strongconnect(w)
komponenty(*w, index, je_ve_stacku, index_pole, nejnizsi_cesta, stack, graf);
//v.lowlink := min(v.lowlink, w.lowlink)
prirad_index(nejnizsi_cesta, v, min(vrat_index(nejnizsi_cesta, v), vrat_index(nejnizsi_cesta, *w)));
}
//else if (w is in S) then
else
{
if (je_ve_stacku[ *w - 1] == true)
{
//v.lowlink := min(v.lowlink, w.index)
prirad_index(nejnizsi_cesta, v, min(vrat_index(nejnizsi_cesta, v), vrat_index(index_pole, *w)));
};
};
++w;
};
};
//if (v.lowlink = v.index) then
if (vrat_index(nejnizsi_cesta, v) == vrat_index(index_pole, v))
{
int w_int = stack.back();
stack.pop_back();
je_ve_stacku[w_int - 1] = false;
cout << w_int << " ";
//until (w = v)
while (w_int != v)
{
w_int = stack.back();
stack.pop_back();
je_ve_stacku[w_int - 1] = false;
// add w to current strongly connected component
cout << w_int << " ";
};
cout << endl;
};
};
void cti_vstup(int vrcholy, vector<vector<int> >& graf)
{
for (int i =0; i < vrcholy; ++i)
{
int pom;
cin >> pom;
vector<int> pompom;
if (pom==0)
{
pompom.push_back(0);
}
while (pom!=0)
{
pompom.push_back(pom);
cin >> pom;
};
graf.push_back(pompom);
};
};
int main()
{
int vrcholy;
cin >> vrcholy;
vector<vector<int> > graf;
cti_vstup(vrcholy, graf);
vector<int> index_pole, nejnizsi_cesta;
//inicializace indexu jednotlivych vrcholu
for (int i =0; i < vrcholy; ++i)
{index_pole.push_back (-1);};
for (int i =0; i < vrcholy; ++i)
{nejnizsi_cesta.push_back(-1);};
vector<int> stack;
vector<bool> je_ve_stacku(vrcholy, false);
int index = 0;
for (int i = 1; i <= vrcholy; ++i)
{
if (vrat_index(index_pole, i) == -1)
{
komponenty(i, index, je_ve_stacku, index_pole, nejnizsi_cesta, stack, graf);
};
};
return 0;
}