Cod sursa(job #2419429)

Utilizator bogikanagyNagy Boglarka bogikanagy Data 8 mai 2019 14:34:13
Problema Ciclu Eulerian Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
//#include <iostream>
#include <fstream>
#include <vector>
#include <deque>

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

#define s second
#define f first

struct el
{
    int p,h;
    bool l;
};

struct adat
{
    long long fo;
    vector <el> sz;
};
vector <adat> x;

deque <int> y;
long long n,m,i,j,a,b;
vector <int> lat;

int euler (int csp)
{
    int j;
    el k;
    for (j=0;j<=x[csp].sz.size()-1;++j)
    {
        k=x[csp].sz[j];
        if (!k.l)
        {
            x[csp].sz[j].l=true;
            x[k.p].sz[k.h].l=true;
            euler(k.p);
        }
    }
    y.push_front(csp);
}
int main()
{
    cin>>m>>n;
    x.resize(m+1);
    lat.resize(m+1);
    for (i=1;i<=n;++i)
    {
        cin>>a>>b;
        x[a].fo++;
        x[b].fo++;
        if (a!=b)
         {
             x[a].sz.push_back({b,x[b].sz.size(),false});
             x[b].sz.push_back({a,x[a].sz.size()-1,false});
         }
        else
         {
             x[a].sz.push_back({b,x[b].sz.size()+1,false});
             x[b].sz.push_back({a,x[b].sz.size()-1,false});

         }
    }

/*    for(i=1;i<=m;++i)
    {
        cout<<i<<": ";
        for(auto e:x[i].sz)
            cout<<e.p<<" "<<e.h<<" ";
        cout<<"\n";
    }*/

    bool fok;
    fok=true;
    for (i=1;i<=m;++i) if (x[i].fo%2!=0) fok=false;

    if (!fok) cout<<"-1";
    else
    {
        euler(1);
        for (auto e:y) cout<<e<<" ";
    }
    return 0;
}