Cod sursa(job #2439727)

Utilizator AlexandruGabrielAliciuc Alexandru AlexandruGabriel Data 16 iulie 2019 18:38:20
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
#define N 100005

using namespace std;

ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");

int n, x, y, m;
list <int> L[N];
vector <int> ciclu;
stack <int> st;

void Euler()
{
    st.push(1);

    while (!st.empty())
    {
        int nod = st.top();

        if (L[nod].size() == 0)
        {
            ciclu.push_back(nod);
            st.pop();
        }
        else
        {
            int nod2 = L[nod].front();

            L[nod].pop_front();
            L[nod2].erase(find(L[nod2].begin(), L[nod2].end(), nod));

            st.push(nod2);
        }
    }
}

int main()
{
    fin >> n >> m;
    while (m--)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
    {
        if (L[i].size() % 2 == 1)
        {
            fout << -1;
            return 0;
        }
    }

    Euler();
    for (auto &it : ciclu)
        fout << it << " ";
    return 0;
}