Cod sursa(job #2749520)

Utilizator alexoloieriAlexandru Oloieri alexoloieri Data 6 mai 2021 22:50:15
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 kb
#include <fstream>
#include <vector>
#include <utility>
#include <stack>
#define nmax int(1e5) + 5
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int n, m, x, y;
vector<pii> G[nmax];
int sol[nmax * 5];
bool seen[nmax * 5];
bool used[nmax * 5];

void dfs(int k) {
    while (G[k].size()) {
        auto act = G[k].back();
        G[k].pop_back();
        if (used[act.second]) {
            continue;
        }
        used[act.second] = true;
        dfs(act.first);
    }
    fout << k << ' ';
}

int main()
{
    fin >> n >> m;
    for (int i=1;i<=m;++i) {
        fin >> x >> y;
        G[x].pb(mp(y, i));
        G[y].pb(mp(x, i));
    }
    for (int i=1;i<=n;++i) {
        if (G[i].size() % 2) {
            fout << "-1\n";
            return 0;
        }
    }
    stack<int> st;
    st.push(1);
    int seenNodes = 0;
    while (st.size()) {
        auto act = st.top();
        if (!seen[act]) {
            seen[act] = true;
            ++seenNodes;
        }
        if (G[act].size()) {
            auto node = G[act].back();
            G[act].pop_back();
            if (used[node.second]) {
                continue;
            }
            used[node.second] = true;
            st.push(node.first);
            continue;
        } else {
            st.pop();
            sol[++sol[0]] = act;
        }
    }
    if (seenNodes < n) {
        fout << "-1\n";
    } else {
        for (int i=1;i<=sol[0];++i) {
            fout << sol[i] << ' ';
        }
        fout << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}