Cod sursa(job #1473076)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 18 august 2015 15:18:25
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.4 kb
#define REP(a,b) for(int a=0; a<(b); ++a)
#define REP2(a,b) for(int a=1; a<=(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b)-1; a>=(c); --a)
#define BCK2(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define FILL(x,y) memset(x,y,sizeof(x))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define x first
#define y second
#define st first
#define nd second
#define pb push_back
#define l(n) (n<<1)
#define r(n) ((n<<1)+1)
#define f(n) (n>>1)
#define lsb(a) (a&-a)

#include<vector>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;
#ifndef ONLINE_JUDGE
#include<fstream>
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
#else
#include<iostream>
#endif

const int NMAX = 100069;
const int dx[] = {0, 0, -1, 1}; //1,1,-1,1};
const int dy[] = {-1, 1, 0, 0}; //1,-1,1,-1};

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef pair<K, K> PKK;
typedef vector<int> VI;

int x,y;
int N,M;

int deg[NMAX];
int viz[NMAX];

VI G[NMAX];

void BFS()
{
    int q[NMAX],c=0,v=0;
    q[v++]=1;
    while(c<v)
    {
        int i=q[c]; c++;
        REP(j,G[i].size())
            if(!viz[G[i][j]])
                q[v++]=G[i][j], viz[G[i][j]]=true;
    }
}

void euler()
{
    int s[NMAX],v; // stack
    int p[NMAX],r; // stack
    s[v++]=1;
    while(v>0)
    {
        int i=s[v-1]; v--;
        cout<<"attempting euler for "<<i<<" with "<<G[i].size()<<" edges \n";
        while(G[i].size())
        {
            cout<<"doing euler for "<<i<<"\n";
            s[v++]=i;
            
            int j=G[i][0];
            // erase edge
            G[i].erase(G[i].begin());
            for(VI::iterator k=G[j].begin(); k!=G[j].end(); ++k)
                if(*k == i){
                    G[j].erase(k); break;
                }
            i=j;
        }
        p[r++]=i;
    }
    BCK(i,r,0)
        cout<<p[i]<<" ";
}

int main()
{
    cin>>N>>M;
    REP(i,M)
    {
        cin>>x>>y;
        deg[x]++, deg[y]++;
        G[x].pb(y), G[y].pb(x);
    }
    
    // check if it's eulerian graph:
    BFS();
    REP2(i,N)
        if(!viz[i] || deg[i]%2==1){
            cout<<"-1"; return 0;
        }
    
    euler();
    
    return 0;
}