Pagini recente » Cod sursa (job #205439) | Istoria paginii utilizator/tennismath | Cod sursa (job #2292556) | Profil florinhaja | Cod sursa (job #1053989)
#include<fstream>
#include<vector>
#include<stack>
using namespace std;
#define max_n 100010
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct muchie{
int x , m;
}mc;
int n , m , x , y , nod;
int Out[max_n * 5];
vector< muchie > L[max_n];
vector<int> Sol;
stack<int>S;
void read(){
f>>n>>m;
for( int i = 1 ; i <= m ; i++ ){
f>>x>>y;
mc.x = y; mc.m = i;
L[x].push_back(mc);
mc.x = x;
L[y].push_back(mc);
}
}
bool graf_euler(){
for( int i = 1 ; i <= n ; i++ )
if( L[i].size() % 2 == 1 )
return false;
return true;
}
void solve(){
S.push(1);
while( S.size() ){
nod = S.top();
if( L[nod].size() != 0 ){
if( Out[ L[nod].back().m ] )
L[nod].pop_back();
else{
S.push( L[nod].back().x );
Out[ L[nod].back().m ] = 1;
L[nod].pop_back();
}
}
else{
Sol.push_back(nod);
S.pop();
}
}
int i = 1;
for( vector<int>::iterator it = Sol.begin() ; i <= m && it != Sol.end() ; i++ , it++ )
g<<(*it)<<" ";
}
int main(){
read();
if( graf_euler() )
solve();
else
g<<"-1\n";
return 0;
}