Cod sursa(job #1830374)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 16 decembrie 2016 16:56:26
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <stack>
#include <algorithm>
#include <vector>
#define fa_numar {nr = 0; while (s[j] >= '0' && s[j] <= '9') nr = nr*10+s[j]-'0', j++; while (s[j] == ' ') j++;}

using namespace std;

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

int n, m, i, j, x, y, l, nr;
bool viz[100005];
char s[100];
vector<int> ls[100005];
int dim[100005];
bool fol[100005];

stack <int> stiva;
int main() {
    f >> n >> m; f.get();
    while (m--) {
        f.getline(s, sizeof(s));
        j = 0; fa_numar; x = nr;
        fa_numar; y = nr;
        //g << x << ' ' << y <<
        ls[x].push_back(y);
        ls[y].push_back(x);
        dim[x]++, dim[y]++;
    }
    for (i = 1; i <= n; i++)
        if (dim[i]% 2 == 1) {
            g << -1;
            return 0;
        }

    stiva.push(1);
    while (stiva.empty() == 0) {
        x = stiva.top(), l = ls[x].size();
        fol[x] = 0;
        if (l == 0) {
            g << stiva.top() <<' ';
            stiva.pop();
            continue;
        }
        y = ls[x][l-1];
        ls[x].pop_back();
        ls[y].erase(find(ls[y].begin(), ls[y].end(), x));
        if (!fol[y]) {
            stiva.push(y);
            fol[y] = 1;
        }
    }

    return 0;
}