Cod sursa(job #2056036)

Utilizator horiacoolNedelcu Horia Alexandru horiacool Data 4 noiembrie 2017 01:25:37
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define NMAX 100010
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int N,M,i,x,y;
vector<int> v[NMAX];
stack<int>st;
int main()
{
    f>>N>>M;
    for(i = 1 ; i <= M ; ++i) f>>x>>y , v[x].push_back(y) , v[y].push_back(x);
    for(i = 1 ; i <= N ; ++i)
        if( v[i].size()%2 )
        {
            g << -1 ;
            return 0;
        }
    st.push(1);
    while( !st.empty() )
    {
        x=st.top();
        while( !v[x].empty() )
        {
            y = v[x].back();
            v[x].pop_back();
            v[y].erase( find(v[y].begin(),v[y].end(),x) );
            st.push(y);
            x=y;
        }
        g<<st.top()<<' ';
        st.pop();
    }
    return 0;
}