Pagini recente » Cod sursa (job #1899685) | Cod sursa (job #1886258) | Cod sursa (job #895425) | Cod sursa (job #1274950) | Cod sursa (job #1907605)
#include <fstream>
#include <queue>
#include <stack>
#include <list>
using namespace std;
ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");
const int Nmax = 100001;
int n, m, nxt, crt, grad[ Nmax ];
bool viz[ Nmax ];
list < int > graph[ Nmax ], solution;
stack < int > S;
void Read()
{
int i, x, y;
f >> n >> m;
for ( i = 1; i <= m; ++ i )
{
f >> x >> y;
graph[ x ].push_back( y );
graph[ y ].push_back( x );
grad[ x ] ++;
grad[ y ] ++;
}
}
bool isEuelerian()
{
int i;
queue < int > Q;
Q.push( 1 );
viz[ 1 ] = true;
while ( !Q.empty() )
{
crt = Q.front();
Q.pop();
for ( list < int > :: iterator it = graph[ crt ].begin(); it != graph[ crt ].end(); it ++ )
{
if ( !viz[ *it ] )
{
viz[ *it ] = true;
Q.push( *it );
}
}
}
for ( i = 1; i <= n; ++ i )
{
if ( !viz[ i ] )
return false;
if ( (grad[ i ] & 1) || !grad[ i ] )
return false;
}
return true;
}
void kick( int crt, int nxt )
{
list < int > :: iterator it;
for ( it = graph[ nxt ].begin(); *it != crt; it ++ );
graph[ nxt ].erase( it );
}
void Solve()
{
if ( isEuelerian() )
{
S.push( 1 );
while ( !S.empty() )
{
crt = S.top();
if ( !graph[ crt ].empty() )
{
nxt = *graph[ crt ].begin();
S.push( nxt );
kick( crt, nxt );
graph[ crt ].erase( graph[ crt ].begin() );
}
else
{
solution.push_back( S.top() );
S.pop();
}
}
for ( list < int > :: iterator it = solution.begin(); it != --solution.end(); it ++ )
g << *it << ' ';
}
else
{
g << "-1";
}
}
int main()
{
Read();
Solve();
return 0;
}