Cod sursa(job #2927992)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 21 octombrie 2022 22:40:36
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <algorithm>
#include <fstream>
#include <list>
using namespace std;

ifstream cin( "ciclueuler.in" );
ofstream cout( "ciclueuler.out" );

const int MAX = 1e5 + 10;
list<int> l[ MAX ];
int st[ MAX * 5 ];
int n, m, x, y;

int main()
{
    cin >> n >> m;
    for( int i = 1; i <= m; ++i ) {
        cin >> x >> y;
        l[ x ].push_back( y );
        l[ y ].push_back( x );
    }

    bool ok = true;
    for( int i = 1; i <= n; ++i )
        if( l[ i ].size() & 1 ) 
            ok = false;

    if( ok ) {
        int poz = 1;
        st[ poz ] = 1;
        while( poz > 0 ) {
            x = st[ poz ];
            while( !l[ x ].empty() ) {
                y = l[ x ].back();
                l[ x ].pop_back();
                l[ y ].erase( find( l[ y ].begin(), l[ y ].end(), x ) );
                st[ ++poz ] = y;
                x = y;
            }
            cout << st[ poz-- ] << ' ';
        }
        cout << '\n';
    } else cout << "-1\n";
    return 0;
}