Cod sursa(job #2527453)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 20 ianuarie 2020 13:20:54
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.49 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <assert.h>

const int MAXN = 100000 + 1;

using namespace std;

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

template<class T>
struct Nod{
    T value;
    Nod *urm;
    Nod(){
        urm = nullptr;
    }
};
template<class T>
class MyList{
public:
    Nod<T> *first;
    Nod<T> *last;
    int elem;

    MyList(){
        last = new Nod<T>();
        first = new Nod<T>();
        elem = 0;
    }
    void push_back(T value){
        Nod<T> *urmator = new Nod<T>();
        urmator->value = value;
        last->urm = urmator;
        last = urmator;
        elem++;
        if(elem == 1){
            first = last;
        }
    }
};

bool viz[500005];
int grad[MAXN];
int stiva[5*MAXN];
vector<pair<int,int> >grafa[MAXN];
MyList<pair<int,int> >graf[MAXN];
int n,m;

void dfs(int nod){
//    for(auto pereche : graf[nod]){
//        int curent = pereche.first;
//        int label = pereche.second;
//        if(!viz[label]){
//            viz[label] = true;
//            dfs(curent);
//            out<<nod<<" ";
//        }
//    }

}
inline void dfs_stiva(int start){
    int varf = 0;
    stiva[++varf] = start;
    while(varf > 0){
        bool are_vecini = false;
        int nod = stiva[varf];
        Nod<pair<int,int> > *it = graf[nod].first;
        while(it != nullptr){
            int curent = it->value.first;
            int label =  it->value.second;
            if(!viz[label]){
                are_vecini = true;
                viz[label] = true;
                stiva[++varf] = curent;
                break;
            }
            it = it->urm;
        }

        if(!are_vecini){
            --varf;
            out<<nod<<' ';
        }
    }
}

int main()
{
    in.tie(0);
    out.tie(0);
    ios::sync_with_stdio(false);
    in>>n>>m;
    for(int i = 1; i <= m; ++i){
        int x,y;
        in>>x>>y;
        graf[x].push_back(make_pair(y,i));
        graf[y].push_back(make_pair(x,i));
        ++grad[x];
        ++grad[y];
    }


    bool ok = true;
    for(int i = 1; i <= n && ok; ++i)
        if(grad[i] % 2)
            ok = false;
    if(!ok){
        out<<-1;
        return 0;
    }


//    Nod<pair<int,int> > *it = graf[2].first;
//    while(it != nullptr){
//        int curent = it->value.first;
//        cout<<curent<<" ";
//        it = it->urm;
//    }
    dfs_stiva(1);
    return 0;
}