Cod sursa(job #2991299)

Utilizator isariandrewIsari Andrew isariandrew Data 9 martie 2023 11:23:32
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
#include <algorithm>
#include <vector>

#define LG(x) ((int) (x).size())
#define NMAX 100005
#define MMAX 500005
using namespace std;

ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");

int n, m, x, y;
int from[MMAX], to[MMAX];

vector<int> G[NMAX];
vector<int> ans;
vector<int> stk;

bool usedEdge[MMAX];

int main()
{
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        cin>>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 (LG(G[i]) & 1) {
            cout<<"-1"<<'\n';
            return 0;
        }
    }
    
    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<LG(ans)-1; i++)
    {
        cout<<ans[i]<<' ';
    }
    cout<<'\n';
}