Cod sursa(job #2926099)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 16 octombrie 2022 22:37:59
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 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;

    int poz = 1;
    st[ poz ] = 1;
    while( poz > 0 ) {
        x = st[ poz ];
        while( !l[ x ].empty() ) {
            y = l[ x ].front();
            l[ x ].pop_front();
            l[ y ].erase( find( l[ y ].begin(), l[ y ].end(), x ) );
            st[ ++poz ] = y;
            x = y;
        }
        cout << st[ poz-- ] << ' ';
    }
    return 0;
}