Cod sursa(job #1290576)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 decembrie 2014 15:17:27
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 2.52 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "ciclueuler";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = 666013;

const int NMAX = 100000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;

int N, M;
vector<int> V[NMAX];
vector<int> S;
vector<int> ciclu;
bitset<NMAX> viz;

void dfs(int x) {
    viz[x] = 1;
    for(auto y : V[x])
        if(!viz[y])
            dfs(y);
}

int eulerian() {
    int i;

    dfs(1);

    for(i = 1; i <= N; i++)
        if(V[i].size() % 2 || !viz[i])
            return 0;

    return 1;
}

void sterge_muchie(int x, int y) {
    for(auto &z : V[x])
        if(z == y) {
            swap(V[x].back(), z);
            V[x].pop_back();
            return;
        }
}

void cauta_ciclu() {
    int x, y;

    S.push_back(1);

    while(!S.empty()) {
        x = S.back();

        if(V[x].empty()) {
            ciclu.push_back(x);
            S.pop_back();
        } else {
            y = V[x][0];
            sterge_muchie(x, y);
            sterge_muchie(y, x);
            S.push_back(y);
        }
    }
}

int main() {
    int x, y;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d%d", &N, &M);

    while(M--) {
        scanf("%d%d", &x, &y);
        V[x].push_back(y);
        V[y].push_back(x);
    }

    if(eulerian())
        cauta_ciclu();
    else
        printf("-1\n");

    ciclu.pop_back();

    for(auto x : ciclu)
        printf("%d ", x);

    return 0;
}