Cod sursa(job #2760025)

Utilizator levladiatorDragutoiu Vlad-Ioan levladiator Data 22 iunie 2021 15:12:22
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
#define ull unsigned long long
#define x first
#define y second
#define pi pair<int,int>
#define pl pair<ll,ll>
#define EPSILON 0.000001
#define zeros(x) ((x^(x-1))&x)

using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

const double EPS=1e-9;
const ll NMAX=1e5+5,INF=1e18,MOD=998244353,inf=INT_MAX,MMAX=5e5+5;


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

bool usedEdge[MMAX];
int from[MMAX],to[MMAX];
int N,M;

int main()
{
    fin>>N>>M;
    for(int i=1;i<=M;i++)
    {
        int a,b;
        fin>>a>>b;
        G[a].pb(i);
        G[b].pb(i);
        from[i]=a;
        to[i]=b;
    }
    for(int i=1;i<=N;i++)
    {
        if((G[i].size())&1)
        {
            fout<<-1;
            return 0;
        }
    }
    stk.pb(1);
    while(!stk.empty())
    {
        int node=stk.back();
        if(!G[node].empty())
        {
            int edge=G[node].back();
            G[node].pop_back();
            if(!usedEdge[edge])
            {
                usedEdge[edge]=true;
                if(to[edge]!=node)stk.pb(to[edge]);
                else stk.pb(from[edge]);
            }
        }
        else
        {
            stk.pop_back();
            ans.pb(node);
        }
    }
    for(int i=0;i<ans.size()-1;i++)
    {
        fout<<ans[i]<<' ';
    }

    return 0;
}