Cod sursa(job #1126826)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 27 februarie 2014 09:53:31
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.81 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const char infile[] = "ciclueuler.in";
const char outfile[] = "ciclueuler.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

Graph G;
stack <int> st;
int N, M;

void Euler() {
    st.push(1);
    while(!st.empty()) {
        int Node = st.top();
        if(G[Node].size()) {
            int newNode = G[Node].back();
            G[Node].pop_back();
            G[newNode].erase(find(G[newNode].begin(), G[newNode].end(), Node));
            st.push(newNode);
        }
        else {
            st.pop();
            if(!st.empty())
                fout << Node << ' ';
        }
    }
}

inline void CheckEulerProperty() {
    for(int i = 1 ; i <= N ; ++ i)
        if(!G[i].size() || G[i].size() & 1) {
            fout << "-1\n";
            exit(0);
        }
}

int main() {
    fin >> N >> M;
    for(int i = 1 ; i <= M ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    CheckEulerProperty();
    Euler();
    fin.close();
    fout.close();
    return 0;
}