Cod sursa(job #1963018)

Utilizator ggaaggaabbiigoteciuc gabriel ggaaggaabbii Data 12 aprilie 2017 11:13:14
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define MAXN 100010
#define maxb 100000
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
char buf[maxb];
vector <int> G[MAXN],ans;
stack <int> st;
int viz[MAXN],n,m,gr[MAXN],x,y,w,urm,nr,poz(maxb);
int getint()
{
    int nr=0;
    while(buf[poz]<'0'||buf[poz]>'9')
        if(++poz>=maxb)
        {
            f.read(buf,maxb);
            poz=0;
        }
    while(buf[poz]>='0'&&buf[poz]<='9')
    {
        nr=nr*10+buf[poz]-'0';
        if(++poz>=maxb)
        {
            f.read(buf,maxb);
            poz=0;
        }
    }
    return nr;
}
int main()
{
    n=getint();
    m=getint();
    while(m--)
    {
        x=getint();
        y=getint();
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i=1; i<=n; ++i)
    {
        if(G[i].size()%2)
        {
            g<<-1;
            return 0;
        }
    }
    st.push(1);
    while(st.size())
    {
        w=st.top();
        if(!G[w].empty())
        {
            urm=G[w][G[w].size()-1];
            G[w].pop_back();
            st.push(urm);
            nr=G[urm].size()-1;
            for(auto &it:G[urm])
                if(it==w)
                {
                    swap(it,G[urm][nr]);
                    G[urm].pop_back();
                    break;
                }
        }
        else
        {
            ans.push_back(w);
            st.pop();
        }
    }
    ans.pop_back();
    for(auto it:ans)
        g<<it<<' ';
    return 0;
}