Pagini recente » Cod sursa (job #2851842) | Cod sursa (job #548165) | Cod sursa (job #2977790) | Cod sursa (job #2440275) | Cod sursa (job #1841328)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>
#define nrmax 100001
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
stack < int > st;
stack < int > sol;
bool used[nrmax];
int grad[nrmax];
vector < int > G[nrmax];
int n , m , x ,y , ok;
void dfs ( int i )
{
used[i] = 1;
for ( vector < int > ::iterator j = G[i].begin(); j != G[i].end() ; j++ )
{
if ( !used[ (*j) ])
dfs( (*j) );
}
}
void ciclu_eulerian(int i)
{
st.push(i);
while ( !st.empty() )
{
int nod = st.top();
if ( G[nod].size() )
{
int vecin = G[nod][0];
G[nod].erase(G[nod].begin());
G[vecin].erase( find(G[vecin].begin(), G[vecin].end() , nod)) ;
st.push(vecin);
}
else
{
st.pop();
sol.push(nod);
}
}
}
int main()
{
f >> n >> m ;
for ( ; m-- ; )
{
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
grad[x]++;
grad[y]++;
}
dfs(1);
for ( int i = 1; i <= n ; i++ )
{
if ( !used[i] or grad[i]%2==1 )
{
g << -1;
return 0;
}
}
ciclu_eulerian(1);
while( !sol.empty() )
{
int nod = sol.top();
sol.pop();
if ( !sol.empty() )
g << nod << " " ;
}
return 0;
}