Cod sursa(job #1918053)

Utilizator hantoniusStan Antoniu hantonius Data 9 martie 2017 14:00:37
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#define maxn 100005
#define maxm 500005
using namespace std;

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

int n, m, stiva[maxm], vf;
list <int> l[maxn];

void read()
{
    int x, y;

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

void solve()
{
    int x, y;

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

int main()
{
    read();

    //verificare ciclu eulerian
    for (int i = 1; i <= n; i++) {
        if (l[i].size() % 2 == 1) {
            fout << -1;
            return 0;
        }
    }

    solve();
    return 0;
}