Cod sursa(job #2046622)

Utilizator andru47Stefanescu Andru andru47 Data 23 octombrie 2017 22:25:16
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100005;
int n , m;
vector <int> muchii[NMAX];
bool ap[5 * NMAX];
stack <int> st;
pair <int ,int> muchie[5 * NMAX];
vector <int> res;
inline void parcurgere_euleriana()
{
    for (int i = 1; i<=n; ++i)
        if (muchii[i].size() & 1)
        {
            printf("-1");
            return ;
        }
    st.push(1);
    while (!st.empty())
    {
        int top = st.top();
        if (muchii[top].size() == 0)
        {
            res.push_back(top);
            st.pop();
            continue;
        }
        //st.pop();
        int muchiee = muchii[top].back();
        muchii[top].pop_back();
        if (ap[muchiee])
            continue;
        ap[muchiee] = true;
        st.push(muchie[muchiee].first != top ? muchie[muchiee].first : muchie[muchiee].second);
    }
    res.pop_back();
    for (auto &it : res)
        printf("%d ", it);
    return ;
}
int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);

    scanf("%d %d\n", &n, &m);
    for (int i = 1; i<=m; ++i)
    {
        int x , y;
        scanf("%d %d", &x, &y);
        muchii[x].push_back(i);
        muchii[y].push_back(i);
        muchie[i] = {x , y};
    }

    parcurgere_euleriana();

    return 0;
}