Cod sursa(job #1968650)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 17 aprilie 2017 19:44:18
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <list>
#include <algorithm>

using namespace std;

ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");

const int nmax = 100005;
int n, m, i, j, x, y;
int stiva[100*nmax], vf;
list<int> ls[nmax];
list<int>::iterator w;

int main() {
    f >> n >> m;
    for (i = 1; i <= m; i++) {
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (ls[i].size()%2) {g<<-1;return 0;}

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