Cod sursa(job #1889774)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 22 februarie 2017 21:21:56
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

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

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

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

int main() {
    f >> n >> m;
    while (m--) {
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
    }
    df(1);
    for (i = 1; i<= n; i++)
        if (viz[i] == 0 || ls[i].size()%2==1) {
            g << -1;
            return 0;
        }
    stiva.push(1);
    ok = 1;
    while (stiva.empty()==0) {
        x = stiva.top();
        while (ls[x].size() > 0) {
            y = ls[x].front();
            stiva.push(y);
            ls[x].erase(ls[x].begin());
            ls[y].erase(find(ls[y].begin(), ls[y].end(), x));
            x = y;
            //g<<1;
        }
        //g<<'\n';
        sol.push_back(stiva.top());
        stiva.pop();
    }
    for (auto &it:sol)
        g << it<< " ";

    return 0;
}