Cod sursa(job #2419127)

Utilizator HannaLieb Hanna Hanna Data 7 mai 2019 18:19:38
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <fstream>
#include <vector>
#include <deque>
#include <stack>

using namespace std;

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

int n,m,i,a,b;

struct el
{
    int p,h;    ///p-az el masik vege, h-az adott csp helye a p elei kozott
    bool l; ///l-vegig mentem-e mar azon az elen
};

struct adat
{
    int t;  ///t-hol tratok az e vektorban
    vector<el>e;    ///hova vezet ut az adott csp-bol
};
vector<adat>x;

deque<int>megold;
//stack<int>s;

void euler(int csp)
{
    int i;
    el k;
    //s.push(csp);
    for(i=x[csp].t;i<x[csp].e.size();++i)
    if(!x[csp].e[i].l)
    {
        k=x[csp].e[i];
        //if(!k.l)
        //{
            x[csp].t=i;
            x[csp].e[i].l=1;
            x[k.p].e[k.h].l=1;
            euler(k.p);
        //}
    }
    else if(i<x[csp].t) i=x[csp].t;

    megold.push_front(csp);
    //s.pop();
}

int main()
{
    cin>>n>>m;
    x.resize(n+1);
    for(i=1;i<=m;++i)
    {
        cin>>a>>b;
        if(a!=b)
        {
            x[a].e.push_back({b,x[b].e.size(),0});
            x[b].e.push_back({a,x[a].e.size()-1,0});
        }
        else
        {
            x[a].e.push_back({b,x[b].e.size()+1,0});
            x[b].e.push_back({a,x[a].e.size()-1,0});
        }
    }

    bool ok=true;
    for(i=1;i<=n;++i)
    if(x[i].e.size()%2)
    {
        ok=false;
        break;
    }
   /* else{
        for(auto f : x[i].e) cout<<f.p<<" "<<f.h<<"   ";
        cout<<"\n";
    }*/

    if(!ok) cout<<"-1\n";
    else
    {
        euler(1);
        megold.pop_back();
        for(auto e : megold) cout<<e<<" ";
    }


    return 0;
}