Pagini recente » Cod sursa (job #2499287) | Cod sursa (job #2041748) | Cod sursa (job #1234274) | Cod sursa (job #932649) | Cod sursa (job #2006712)
#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;
list< 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, list< int >::iterator it )
{
//++cnt;
// if( cnt % 10000 == 0 ) cout << cnt << "\n";
int nnod = *it;
graph[nod].erase( it );
list< int >::iterator it2;
for( it2 = graph[nnod].begin(); it2 != graph[nnod].end(); ++it2 )
if( *it2 == nod )
{
graph[nnod].erase( it2 );
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].begin());
removeEdge( *it, graph[*it].begin() );
cic.push( cur );
while( cur != *it )
{
//cout << *it << cur << "\n";
/*/
cout << *it << " " << cur << "\n";
for( int h = 1; h <= 3; ++h )
{
cout << " " << h << ": ";
list< int >::iterator k;
for( k = graph[h].begin(); k != graph[h].end(); ++k )
cout << *k << " ";
cout << "\n";
}
//*/
int l = cur;
cur = *graph[cur].begin();
removeEdge( l, graph[l].begin() );
cic.push( cur );
}
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;
}