Cod sursa(job #2847351)

Utilizator Rares5000Baciu Rares Rares5000 Data 10 februarie 2022 18:03:58
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <stack>
#include <vector>

using namespace std;

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

stack<int> st;
vector< pair<int, int> > v[100002];
vector<int> ans;
int fr[500002], n, m;

int main()
{
    int i, x, y, M;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        v[x].push_back({y, i});
        v[y].push_back({x, i});
    }
    for(i = 1; i <= n; i++)
        if(v[i].size() % 2 != 0)
        {
            fout << "-1";
            return 0;
        }
    st.push(1);
    while(!st.empty())
    {
        int nod = st.top();
        if(v[nod].size() != 0)
        {
            M = v[nod][v[nod].size() - 1].second;
            if(fr[M] == 0)
            {
                fr[M] = 1;
                st.push(v[nod][v[nod].size() - 1].first);
            }
            v[nod].pop_back();
        }
        else
        {
            st.pop();
            ans.push_back(nod);
        }
    }
    ans.pop_back();
    for(auto w : ans)
        fout << w << " ";
    return 0;
}