Zdravim, nevite prosim, co v tomto kodu by mohlo zpusobovat segmantation fault? Jedna se o hledani Eulerovske cesty v grafu. S jednoduchymi daty to funguje, ale kdyz se nad tim pusti obrovska testovaci data (mne neznama), tak to vyhodi segfault. Nevim jak mam odhalit zdroj, kdyz nevim cim presne to otestovat, zkousel jsem vsechny "specialni" varianty zadani, ktere me napadly... Diky moc.
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
class Node {
int id;
int in;
int out;
vector<int> succ;
public:
Node();
Node(int i);
int getId();
vector<int> getSuccs();
void addSucc(int successor);
void updateSucc(int successor, int index);
void delSucc(int i);
void increaseIn();
void decreaseIn();
void increaseOut();
void decreaseOut();
int getIn();
int getOut();
};
Node::Node(void){
id=-1;
in=0;
out=0;
}
Node::Node(int i){
id=i;
in=0;
out=0;
}
int Node::getId(){
return id;
}
vector<int> Node::getSuccs(){
return succ;
}
void Node::addSucc(int successor){
succ.push_back(successor);
}
void Node::updateSucc(int successor, int index){
succ[index]=successor;
}
void Node::delSucc(int i){
succ.erase(succ.begin()+i);
}
void Node::increaseIn(){
in++;
}
void Node::increaseOut(){
out++;
}
void Node::decreaseIn(){
in--;
}
void Node::decreaseOut(){
out--;
}
int Node::getIn(){
return in;
}
int Node::getOut(){
return out;
}
vector< pair<int,int> > rout;
int M;
int N;
int endNode=-1;
int endOut=0;
int nextNode=-1;
void alg(Node nodes[],int start){
if(nodes[start].getSuccs().size() > 0 && M>0 && start>=0 && start<N){
int lastIndex=nodes[start].getSuccs().size()-1;
int successor=nodes[start].getSuccs()[0];
if(successor==endNode && endOut==0 && M!=1){
int temp=successor;
nodes[start].updateSucc(nodes[start].getSuccs()[lastIndex],0);
nodes[start].updateSucc(temp,(lastIndex));
successor=nodes[start].getSuccs()[0];
}
if(successor==endNode) endOut--;
rout.push_back(make_pair(start,successor));
M--;
nextNode=successor;
if(M>0 && nextNode<N && nextNode>=0){
nodes[start].delSucc(0);
alg(nodes,nextNode);
}
}
}
int main(int argc, char *argv[])
{
cin >> N;
Node *nodes=new Node[N];
for(int i=0;i<N;i++) nodes[i]=Node(i);
int first=0;
int second=0;
for(;;){
cin >> first;
cin >> second;
if(first==0 && second==0) break;
nodes[first].addSucc(second);
nodes[first].increaseOut();
nodes[second].increaseIn();
M++;
}
int tempM=M;
int start=-1;
bool eulerCheck=true;
for(int i=0;i<N;i++){
if(nodes[i].getIn()>nodes[i].getOut() && endNode!=-1){
eulerCheck=false;
break;
}
if(nodes[i].getIn()<nodes[i].getOut() && start!=-1){
eulerCheck=false;
break;
}
if(nodes[i].getIn()>nodes[i].getOut()) { endNode=i; endOut=nodes[i].getOut(); }
else if(nodes[i].getIn()<nodes[i].getOut()) start=i;
}
if(start==-1 || endNode==-1) { start=0; endNode=1; endOut=nodes[1].getOut(); }
if(eulerCheck==true){
alg(nodes,start);
}
if(eulerCheck==false) cout << "-1";
else {
cout << N << endl;
for(unsigned int i=0;i<rout.size();i++) cout << rout[i].first << " " << rout[i].second << endl;
cout << "0 0";
}
}