Cod sursa(job #2159360)

Utilizator Andrei2000Andrei Mihailescu Andrei2000 Data 10 martie 2018 21:26:52
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
#define nmax 100002

using namespace std;

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

vector <pair<int,int>> G[nmax];
vector <int> ans;
stack <int> stk;

int n,m;

int main()
{
    int a,b;
    fin>>n>>m;
    for(int i=1;i<=m;++i){
        fin>>a>>b;
        G[a].push_back({b,G[b].size()});
        G[b].push_back({a,G[a].size()-1});
    }
    for(int i=1;i<=n;++i)
        if(G[i].size() & 1){
            fout<<-1<<'\n';
            return 0;
        }
    stk.push(1);
    cout<<G[1].size();
    while(!stk.empty()){
        int w=stk.top();
        if(!G[stk.top()].empty()){
            int q=G[stk.top()].back().first;
            int qq=G[stk.top()].back().second;
            G[stk.top()].pop_back();
            int ww=G[q].size();
            int www=G[q][qq].first;
            if(G[q].size()>qq){
                stk.push(q);
            }
        }
        else{
            ans.push_back(stk.top());
            stk.pop();
        }
    }
    for(int i=0;i<ans.size()-1;++i)fout<<ans[i]<<' ';
    fout<<'\n';
    return 0;
}