Pagini recente » Cod sursa (job #382452) | Cod sursa (job #2341837) | Cod sursa (job #506728) | Cod sursa (job #1617231) | Cod sursa (job #2866223)
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5;
ifstream fin( "ciclueuler.in" );
ofstream fout( "ciclueuler.out" );
map <int,int> edges[NMAX+1];
stack <int> cpath;
stack <int> epath;
int grad[NMAX+1];
int main() {
int n, m, i, j, a, b, odd, start;
fin >> n >> m;
for( i = 0; i < m; i++ ) {
fin >> a >> b;
edges[a][b]++;
if( a != b )
edges[b][a]++;
grad[a]++;
grad[b]++;
}
/*for( i = 1; i <= n; i++ ) {
for( j = 1; j <= n; j++ )
cout << edges[i][j] << " ";
cout << endl;
}*/
odd = 0;
start = 1;
for( i = 1; i <= n; i++ ) {
if( grad[i] % 2 == 1 ) {
odd++;
start = i;
}
}
if( !( odd == 0 || odd == 2 ) )
fout << -1;
else {
cpath.push( start );
while( !cpath.empty() ) {
if( grad[cpath.top()] == 0 ) {
epath.push( cpath.top() );
cpath.pop();
}
else {
//cout << edges[cpath.top()].begin()->first << " ";
grad[cpath.top()]--;
grad[edges[cpath.top()].begin()->first]--;
int u = edges[cpath.top()].begin()->first;
if( edges[cpath.top()][u] == 1 )
edges[cpath.top()].erase( u );
else
edges[cpath.top()][u]--;
if( u != cpath.top() ) {
if( edges[u][cpath.top()] == 1 )
edges[u].erase( cpath.top() );
else
edges[u][cpath.top()]--;
}
cpath.push( u );
/*for( i = 1; i <= n; i++ ) {
for( auto itr = edges[i].begin(); itr != edges[i].end(); ++itr )
cout << itr->first << " " << itr->second << " ";
cout << endl;
}
cout << endl << endl << endl << endl << endl;*/
}
}
}
while( !epath.empty() ) {
if( epath.size() != 1 )
fout << epath.top() << " ";
epath.pop();
}
return 0;
}