Cod sursa(job #2121851)

Utilizator hantoniusStan Antoniu hantonius Data 4 februarie 2018 13:31:00
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <algorithm>
#define MAX 100001
#define MAXM 500001
using namespace std;

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

list <int> v[MAX];
int n, m;

void read()
{
    int x, y;

    fin >> n >> m;
    for (int i = 0; i < m; ++i) {
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
}

void solve()
{
    int vf = 1, stiva[MAXM], x, y;

    stiva[vf] = 1;
    while (vf > 0) {
        x = stiva[vf];
        while (v[x].empty() == false) {
            y = v[x].front();
            v[x].pop_front();
            v[y].erase(find(v[y].begin(), v[y].end(), x));
            stiva[++vf] = y;
            x = y;
        }
        fout << stiva[vf--] << ' ';
    }
}

int main()
{
    read();
    for (int i = 1; i <= n; ++i)
        if (v[i].size() % 2 == 1) {
            fout << -1;
            return 0;
        }
    solve();
    return 0;
}