Cod sursa(job #2663756)

Utilizator modulopaulModulopaul modulopaul Data 27 octombrie 2020 11:03:49
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.05 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <queue>
#define MAXN 100001
#define MAXM 500001

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

int n,m;

struct muc{
    int dest;
    bool went;
};

struct nod{
    muc mc;
    nod *urm;
};
nod *MC[MAXM];

void add(int x,muc y){
    if(MC[x]==NULL){
        MC[x]=new nod;
        MC[x]->mc=y;
        MC[x]->urm=NULL;
    }
    else{
        nod *q=new nod;
        q->mc=y;
        q->urm=MC[x];
        MC[x]=q;
    }
}

void close(int x,int y){
    nod *i=new nod;
    for(i=MC[x];i!=NULL;i=i->urm){
        if(i->mc.dest==y and i->mc.went==0){
            i->mc.went=1;
            return;
        }
    }
}

void afis(){
    for(int i=1;i<=n;i++){
        nod *j=new nod;
        for(j=MC[i];j!=NULL;j=j->urm){
            fout<<i<<' '<<j->mc.dest<<' '<<j->mc.went<<'\n';
        }
        fout<<"*\n";
    }
}
queue <int> myq;
stack <int> myst;
int grad[MAXN],nrgoodgrad;
int main(){
    fin>>n>>m;
    for(int i=1;i<=m;i++){
        muc x,y;
        fin>>x.dest>>y.dest;
        y.went=0;
        x.went=0;
        grad[x.dest]++;
        if(grad[x.dest]%2==0) nrgoodgrad++;
        else nrgoodgrad--;
        grad[y.dest]++;
        if(grad[y.dest]%2==0) nrgoodgrad++;
        else nrgoodgrad--;
        add(x.dest,y); add(y.dest,x);
    }

    if(nrgoodgrad!=0){
        fout<<-1;
        return 0;
    }
    myst.push(1);
    while(!myst.empty()){
        nod *i=new nod;
        int nod=myst.top();
        bool out=false;
        for(i=MC[nod];i!=NULL and out==false;i=i->urm){
            if(i->mc.went==0){
                myst.push(i->mc.dest);
                i->mc.went=1;
                close(i->mc.dest,nod);
                out=true;
            }
        }
        if(out==false){
            myst.pop();
            myq.push(nod);
        }
    }
    while(myq.size()>1){
        fout<<myq.front()<<' ';
        myq.pop();
    }
    return 0;
}