Cod sursa(job #1141721)

Utilizator valiro21Valentin Rosca valiro21 Data 13 martie 2014 08:40:09
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <cstdio>
#include <vector>

#define NMax 100001

using namespace std;

vector<long> v[NMax], sol;
long x, y, n, m;

void deleteEdge ( long x, long y ) {
    v[x].pop_back ( );
    for ( vector<long>::iterator i = v[y].begin (  ); i != v[y].end ( ); i++ )
        if ( *i == x ) {
            v[y].erase ( i );
            return;
        }
}

void CCEuler ( long now ) {
    long next;
    while ( !v[now].empty (  ) ) {
        next = v[now].back (  );
        deleteEdge ( now, next );
        CCEuler ( next );
    }

    sol.push_back ( now );
}

int main()
{
    freopen ( "ciclueuler.in", "r", stdin );
    freopen ( "ciclueuler.out", "w", stdout );

    scanf ( "%ld %ld", &n, &m );
    for ( long i = 1; i <= m; i++ ) {
        scanf ( "%ld %ld", &x, &y );
        v[x].push_back ( y );
        v[y].push_back ( x );
    }

    CCEuler ( 1 );
    sol.pop_back ( );
    for ( long i = 0; i < sol.size ( ); i++ )
        printf ( "%ld ", sol[i] );

    return 0;
}