Cod sursa(job #1857460)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 26 ianuarie 2017 11:31:38
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <queue>

using namespace std;

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

vector <int> ls[100005];
int n, m, x, y, i, l;
bool viz[100005];
stack<int>stiva;

void bf(int x) {
    int i, l = ls[x].size();
    viz[x] = 1;
    for (i = 0; i < l; i++)
        if (viz[ls[x][i]] == 0)
            bf(ls[x][i]);
}

int main() {
    f >> n >> m;
    while (m--) {
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
    }
    bf(1);
    for (i = 1; i <= n; i++)
        if (ls[i].size()%2 == 1 || viz[i] == 0) {
            g << -1;
            return 0;
        }
    stiva.push(1);
    while (stiva.empty()==0) {
        x = stiva.top(), l = ls[x].size();
        if (l == 0) {
            g << x << ' ';
            stiva.pop();
            continue;
        }
        y = ls[x][l-1];
        stiva.push(y);
        ls[x].pop_back();
        ls[y].erase(find(ls[y].begin(),ls[y].end(), x));
    }

    return 0;
}