Pagini recente » Cod sursa (job #2367320) | Cod sursa (job #1938132) | Cod sursa (job #1165834) | Cod sursa (job #1300436) | Cod sursa (job #1157262)
#include <iostream>
#include <fstream>
#include <queue>
#include <stack>
#include <list>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m, a, b, deg[ 100010 ], viz[ 100010 ];
list<int> v[ 100010 ], L;
queue<int> Q;
stack<int> S;
void read()
{
f >> n >> m;
for(int i = 0; i < m; ++i)
{
f >> a >> b;
v[ a ].push_back(b); deg[ a ]++;
v[ b ].push_back(a); deg[ b ]++;
}
}
void BFS()
{
int aux;
Q.push(1); viz[1] = 1;
while(!Q.empty())
{
aux = Q.front(); Q.pop();
for(list<int> :: iterator it = v[aux].begin(); it != v[aux].end(); ++it)
if(viz[ *it ] == 0)
Q.push(*it), viz[ *it ] = 1;
}
}
bool euler()
{
BFS();
for(int i = 2; i <= n; ++i)
if(viz[ i ] == 0)
return false;
for(int i = 1; i <= n; ++i)
if(deg[ i ] % 2 == 1)
return false;
return true;
}
void sterge(int a, int b)
{
deg[ a ]--; deg[ b ]--;
v[a].pop_front();
for(list<int> :: iterator it = v[b].begin(); it != v[b].end(); ++it)
if(*it == a)
{
v[b].erase(it);
break;
}
}
void ciclu()
{
int aux = 1;
do
{
while(true)
{
if(v[aux].empty())
break;
int aux2 = v[aux].front();
S.push(aux);
sterge(aux, aux2);
aux = aux2;
}
aux = S.top(); S.pop();
L.push_back(aux);
} while( !S.empty() );
for(list<int> :: iterator it = L.begin(); it != L.end(); ++it)
g << *it << " ";
}
int main()
{
read();
if(euler())
ciclu();
else
g << "-1\n";
}