Cod sursa(job #1053988)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 13 decembrie 2013 09:16:41
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include<fstream>
#include<vector>
#include<stack>

using namespace std;

#define max_n 100010

ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");



struct muchie{
    int x , m;
}mc;

int n , m , x , y , nod;
int Out[max_n];

vector< muchie > L[max_n];
vector<int> Sol;
stack<int>S;

void read(){

    f>>n>>m;

    for( int i = 1 ; i <= m ; i++ ){
        f>>x>>y;
        mc.x = y; mc.m = i;
        L[x].push_back(mc);
        mc.x = x;
        L[y].push_back(mc);
    }

}

bool graf_euler(){

    for( int i = 1 ; i <= n ; i++ )
        if( L[i].size() % 2 == 1 )
            return false;
    return true;
}

void solve(){

    S.push(1);

    while( S.size() ){

        nod = S.top();

        if( L[nod].size() != 0 ){
            if( Out[ L[nod].back().m ] )
                L[nod].pop_back();
            else{
                S.push( L[nod].back().x );
                Out[ L[nod].back().m ] = 1;
                L[nod].pop_back();
            }
        }
        else{
            Sol.push_back(nod);
            S.pop();
        }
    }

    int i = 1;

    for( vector<int>::iterator it = Sol.begin() ; i <= m && it != Sol.end() ; i++ , it++ )
        g<<(*it)<<" ";

}

int main(){

    read();

    if( graf_euler() )
        solve();
    else
        g<<"-1\n";

    return 0;
}