Cod sursa(job #2576243)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 6 martie 2020 18:02:39
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int MAXN = 100005, MAXM = 500005;

struct Edge
{
    int x, y;
}edge[MAXM];

vector<int> graph[MAXN], res;
int gr[MAXN], n, m;
bool vis[MAXM];
stack<int> st;

void read()
{
    fin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int x, y;
        fin >> x >> y;
        graph[x].pb(i);
        graph[y].pb(i);
        edge[i] = {x, y};
        ++gr[x], ++gr[y];
    }
}

bool check()
{
    for (int i = 1; i <= n; ++i)
        if (gr[i] & 1)
            return false;
    return true;
}

void solve()
{
    st.push(1);
    while (!st.empty()) {
        int node = st.top();
        if (graph[node].size() == 0) {
            st.pop();
            if (!st.empty())
                fout << node << ' ';
            continue;
        }
        int last = graph[node].back(), from = edge[last].x, to = edge[last].y;
        graph[node].pop_back();
        if (vis[last] == true)
            continue;
        vis[last] = true;
        if (to == node)
            to = from;
        st.push(to);
    }
}

int main()
{
    read();
    if (check() == false)
        fout << "-1\n";
    else
        solve();
    return 0;
}