Cod sursa(job #2844994)

Utilizator StefanBejanBejan Stefan StefanBejan Data 6 februarie 2022 21:07:39
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define N 100000
#define M 500000

using namespace std;

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

int n, m;
int x, y;

vector <int> answer;
vector <pair <int, int> > g[N+5];
stack <int> st;
int f[M+5];

void citire()
{
    fin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        fin >> x >> y;
        g[x].push_back({y, i});
        g[y].push_back({x, i});
    }
}

int main()
{
    citire();
    for(int i = 1; i <= n; ++i)
        if(g[i].size() % 2 == 1)
        {
            fout << -1;
            return 0;
        }
    st.push(1);
    while(!st.empty())
    {
        int nod = st.top();
        int im = g[nod][g[nod].size() - 1].second;

        if(g[nod].size())
        {
            if(f[im] == 0)
            {
                f[im] = 1;
                st.push(g[nod][g[nod].size() - 1].first);
            }

            g[nod].pop_back();
        }
        else
        {
            st.pop();
            answer.push_back(nod);
        }
    }
    answer.pop_back();
    for(int i = 0; i <= answer.size() - 1; ++i)
        fout << answer[i] << " ";
    return 0;
}