Cod sursa(job #1414125)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 2 aprilie 2015 13:13:08
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

int N, M, x, y;
vector < int > G[100005], sol, st;

void euler(int start)
{
    st.push_back(start);
    while (st.size())
    {
        int nod=st.back();
        if (G[nod].size())
        {
            int last=G[nod].back();
            G[nod].pop_back();
            st.push_back(last);

            vector<int>::iterator it=G[last].begin();
            for (; it!=G[last].end(); ++it)
                if (*it==nod)
                {
                    *it=G[last].back();
                    G[last].pop_back();
                    break;
                }
        }
        else sol.push_back(nod), st.pop_back();
    }
}

int main()
{
    f>>N>>M;
    for (int i=1; i<=M; ++i)
    {
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for (int i=1; i<=N; ++i)
        if (G[i].size()&1)
            { g<<"-1\n"; return 0; }

    euler(1);
    for (int i=0; i<(int)sol.size(); ++i)
        g<<sol[i]<<' ';
    return 0;
}