Pagini recente » Cod sursa (job #362371) | Cod sursa (job #2767147) | Cod sursa (job #2299396) | Cod sursa (job #772154) | Cod sursa (job #1915485)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define mp make_pair
#define f first
#define s second
int n, m, x, y, i, crt;
int grad[ 100010 ], it[ 100010 ];
bool viz[ 100010 ], used[ 500010 ];
vector<pair<int, int> > graf[ 100010 ];
stack<int> S;
bool isEuler() {
S.push( 1 );
while ( !S.empty() ) {
crt = S.top();
viz[ crt ] = true;
for ( it[ crt ] ; it[ crt ] < graf[ crt ].size() ; it[ crt ]++ ) {
if ( !viz[ graf[ crt ][ it[ crt ] ].f ] ) {
S.push( graf[ crt ][ it[ crt ] ].f );
break;
}
}
if ( it[ crt ] == graf[ crt ].size() ) {
S.pop();
}
}
for ( i = 1 ; i <= n ; i++ ) {
if ( !viz[ i ] || grad[ i ] % 2 || grad[ i ] == 0 )
return false;
it[ i ] = 0;
}
return true;
}
int main()
{
fin >> n >> m;
for ( i = 1 ; i <= m ; i++ ) {
fin >> x >> y;
graf[ x ].push_back( mp( y , i ) );
graf[ y ].push_back( mp( x , i ) );
grad[ x ]++; grad[ y ]++;
}
if ( isEuler() ) {
S.push( 1 );
while ( !S.empty() ) {
crt = S.top();
for ( it[ crt ] ; it[ crt ] < graf[ crt ].size() ; it[ crt ]++ ) {
if ( !used[ graf[ crt ][ it[ crt ] ].s ] ) {
used[ graf[ crt ][ it[ crt ] ].s ] = true;
S.push( graf[ crt ][ it[ crt ] ].f );
break;
}
}
if ( it[ crt ] == graf[ crt ].size() ) {
if ( S.size() > 1 )
fout << S.top() << ' ';
S.pop();
}
}
} else {
fout << -1;
}
}