Cod sursa(job #1913508)

Utilizator roxanastRoxana Stiuca roxanast Data 8 martie 2017 13:02:43
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>
#define NMAX 100010
#define MMAX 500010
using namespace std;

vector <int> G[NMAX];
bool fol[MMAX];
int n,m,i,from[NMAX],to[NMAX];
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int main()
{
    f>>n>>m;
    for(i=1;i<=m;++i){
        int x,y;
        f>>x>>y;
        G[x].push_back(i);
        G[y].push_back(i);
        from[i]=x;
        to[i]=y;
    }

    bool ok=true;
    for(i=1;i<=n&&ok==true;++i)
        if(G[i].size()&1)
        ok=false;

    if(ok==false)
        g<<"-1\n";
    else{
        vector <int> st;
        vector <int> sol;
        st.push_back(1);
        while(!st.empty()){
            int nod=st.back();
            if(!G[nod].empty()){
                int e=G[nod].back();
                G[nod].pop_back();
                if(!fol[e]){
                    fol[e]=true;
                    int x=from[e];
                    if(x==nod)
                        x=to[e];
                    st.push_back(x);
                }
            }else{
                st.pop_back();
                sol.push_back(nod);
            }
        }

        sol.pop_back();
        for(vector <int>::iterator it=sol.begin();it<sol.end();++it)
            g<<*it<<" ";
    }
    return 0;
}