Cod sursa(job #3169556)

Utilizator CosminDMRCosmin Damureanu CosminDMR Data 15 noiembrie 2023 13:08:42
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include<fstream>
#include<vector>
#include<stack>
#define dim 100001
using namespace std;
ifstream in ("ciclueuler.in" );
ofstream out("ciclueuler.out");
struct muchie {
    int x;
    int y;
    bool f;
};
muchie v[dim*5];
int grad[dim], ciclu[dim], n, m, cnt;
vector<int> G[dim];
stack<int> stiva;
int main() {
    in >> n >> m;
    for (int i = 1, x, y; i <= m; i++) {
        in >> x >> y;
        grad[x]++;
        grad[y]++;
        v[i].x = x;
        v[i].y = y;
        G[x].push_back(i);
        G[y].push_back(i);
    }
    for (int i = 1; i <= n; i++)
        if (grad[i] % 2 == 1) {
            out << -1;
            return 0;
        }
    stiva.push(1);
    while(stiva.empty() == false) {
        int nod = stiva.top();
        int ok = 0;
        while(G[nod].size()) {
            int mch = G[nod].back();
            G[nod].pop_back();
            if (v[mch].f == 0) {
                v[mch].f = 1;
                ok = 1;
                int x = v[mch].x,
                    y = v[mch].y;
                if (x != nod)
                    stiva.push(x);
                else
                    stiva.push(y);
                break;
            }
        }
        if (ok == 0) {
            ciclu[++cnt] = nod;
            stiva.pop();
        }
    }
    if (cnt != m + 1) {
        out << -1;
        return 0;
    }
    for (int i = 1; i < cnt; i++)
        out << ciclu[i] << " ";
}