Pagini recente » Cod sursa (job #2955966) | Cod sursa (job #2643664) | Cod sursa (job #788150) | Cod sursa (job #1962097) | Cod sursa (job #2006626)
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <fstream>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NLIM = 1e5 + 10;
int N, M;
int cnt = 0;
vector< int > graph[NLIM];
vector< int > al[NLIM];
int check( int nod )
{
for( int j = 0; j < graph[nod].size(); ++j )
if( al[nod][j] )
return j;
return -1;
}
void removeEdge( int nod, int j )
{
//++cnt;
// if( cnt % 10000 == 0 ) cout << cnt << "\n";
int nnod = graph[nod][j];
graph[nod].erase( graph[nod].begin() + j );
for( int jj = 0; jj < graph[nnod].size(); ++jj )
if( graph[nnod][jj] == nod )
{
graph[nnod].erase( graph[nnod].begin() + jj );
return;
}
}
int main()
{
fin >> N >> M;
for( int i = 0; i < M; ++i )
{
int x, y;
fin >> x >> y;
graph[x].push_back( y );
graph[y].push_back( x );
al[x].push_back( 1 );
al[y].push_back( 1 );
}
for( int i = 1; i <= N; ++i )
if( graph[i].size() % 2 != 0 )
{
fout << "-1";
return 0;
}
list< int > res;
res.push_back( 1 );
list< int >::iterator it;
for( it = res.begin(); it != res.end(); ++it )
{
// check for non traversed edge
// int edge = check( *it );
if( graph[*it].size() )
{
// get cycle
queue< int > cic;
int cur = graph[*it][0];
removeEdge( *it, 0 );
cic.push( cur );
while( cur != *it )
{
/*/
cout << *it << " " << cur << "\n";
for( int h = 1; h <= 3; ++h )
{
cout << " " << h << ": ";
for( int k = 0; k < graph[h].size(); ++k )
cout << graph[h][k] << " ";
cout << "\n";
}
//*/
int l = cur;
cur = graph[cur][0];
removeEdge( l, 0 );
//res.insert( it, cur );
cic.push( cur );
}
//cout << "end\n";
list< int >::iterator it2 = it;
++it2;
while( cic.size() )
{
res.insert( it2, cic.front() );
cic.pop();
}
}
}
for( it = res.begin(); it != res.end(); ++it )
{
list< int >::iterator it2 = it;
++it2;
if( it2 != res.end() )
fout << *it << " ";
}
return 0;
}