Pagini recente » Cod sursa (job #3270261) | Cod sursa (job #1714910) | Cod sursa (job #1223559) | Cod sursa (job #2580671) | Cod sursa (job #1436938)
#include <fstream>
#include <vector>
#include <stack>
#define nmax 500100
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int N, M, nrInOut[nmax];
bool folosit[nmax];
vector <pair <int, int> > muchi;
vector <int> graf[nmax], Sol;
bool esteInComp[nmax];
bool isEuler(){
for(int i = 1; i <= N; ++i)
if( !nrInOut[i] || nrInOut[i] % 2 != 0 )
return false;
return true;
}
void euler(int nod){
int i;
for(i = 0; i < graf[nod].size(); ++i){
if(! folosit[ graf[nod][i] ]){
int nod2 = muchi[ graf[nod][i] ].first + muchi[ graf[nod][i] ].second - nod;
folosit[ graf[nod][i] ] = true;
euler(nod2);
}
}
Sol.push_back(nod);
}
int main()
{int a, b;
f>>N>>M;
muchi.push_back( make_pair(0, 0) );
for(int i = 1; i <= M; ++i){
f>>a>>b;
muchi.push_back( make_pair(a, b) );
graf[a].push_back(i);
if(a != b){
graf[b].push_back(i);
++nrInOut[a];
++nrInOut[b];
}
}
/*for(int i = 1; i <= N; ++i){
for(int j = 0; j < graf[i].size(); ++j){
g<<muchi[ graf[i][j] ].first<<' '<<muchi[ graf[i][j] ].second<<' ';
}
g<<'\n';
}*/
if( isEuler() ){
euler(1);
for(int i = Sol.size() - 1; i > 0; --i)
g << Sol[i] <<' ';
g<<'\n';
return 0;
}
g<<-1<<'\n';
return 0;
}