Cod sursa(job #2383831)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 19 martie 2019 20:16:52
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

using namespace std;
char BUF[LIM];
int poz;

inline char getChar(){
    poz++;
    if(poz>=LIM){
    	fread(BUF,LIM,1,stdin);
    	poz=0;
    }
    return BUF[poz];
}

inline int getNr(){
    int r=0, semn=1;
    char ch=getChar();
    while(isdigit(ch)==0 && ch!='-') ch=getChar();
    if(ch=='-'){
        semn=-1;
        ch=getChar();
    }
    while(isdigit(ch)!=0){
        r=r*10+semn*(ch-'0');
        ch=getChar();
    }
    return r;
}

vector < pair <int,int> >  G[100001];
vector <int> steve;

int gr[100001],f[100001];

int main()
{
    int n,m,i,x,y;
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    poz=LIM;
    n=getNr();
    m=getNr();
    for(i=1; i<=m; i++)
    {
        x=getNr();
        y=getNr();
        gr[x]++;
        gr[y]++;
        G[x].push_back({y,i});
        G[y].push_back({x,i});
    }
    for(i=1; i<=n; i++)
        if(gr[i]%2)
        {
            printf("-1");
            return 0;
        }
    steve.push_back(1);
    while(!steve.empty())
    {
        int z=steve.back();
        if(!gr[z])
        {
            steve.pop_back();
            if(!steve.empty())
                printf("%d ",z);
            continue ;
        }
        pair <int,int> it=G[z].back();
        G[z].pop_back();
        if(!f[(it.second)])
        {
            f[(it.second)]=1;
            gr[(it.first)]--;
            gr[z]--;
            steve.push_back(it.first);
        }
    }

    return 0;
}