Cod sursa(job #2544100)

Utilizator valentin12Valentin Ion Semen valentin12 Data 11 februarie 2020 19:30:30
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <algorithm>
#include <fstream>
#include <vector>

#define SZ(x) ((int) (x).size())

using namespace std;

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

const int NMAX=100005, MMAX=500005;

vector <int> G[NMAX];

bool usedEdge[MMAX];

int from[MMAX],to[MMAX];

int main()
{
    int n,m;

    f>>n>>m;

    for(int i=0;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;

    }

    for(int i=1;i<=n;i++)
    {

        if(SZ(G[i]) & 1)
        {

            g<<"-1\n";

            return 0;

        }

    }

    vector <int> ans;

    vector <int> stk;

    stk.push_back(1);

    while(!stk.empty())
    {

        int node=stk.back();

        if(!G[node].empty())
        {

            int e=G[node].back();

            G[node].pop_back();

            if (!usedEdge[e])
            {

                usedEdge[e] = true;

                int to = ::from[e] ^ ::to[e] ^ node;

                stk.push_back(to);

            }

        }
        else
        {

            stk.pop_back();

            ans.push_back(node);

        }

    }



    for(int i=0;i<SZ(ans)-1;i++)
    {

        g<<ans[i]<<' ';

    }

    g<<'\n';

return 0;
}