Cod sursa(job #2496295)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 20 noiembrie 2019 17:21:41
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");

constexpr int NMAX = 1e5 + 5;
constexpr int MMAX = 5e5 + 5;

vector <int> sol;

vector <int> G[NMAX];
bool elim[MMAX];

struct muchie
{
    int x;
    int y;
};

muchie a[MMAX];

int main()
{
    int n, m;

    f >> n >> m;

    for (int i = 1; i <= m; ++i)
    {
        int x, y;
        f >> x >> y;

        a[i] = {x, y};
        G[x].push_back(i);
        G[y].push_back(i);
    }

    for (int i = 1; i <= n; ++i)
    {
        if (G[i].size() % 2 == 1)
        {
            g << -1 << '\n';
            return 0;
        }
    }

    vector <int> v;
    v.push_back(1);

    while (!v.empty())
    {
        int nod = v.back();

        if (!G[nod].empty())
        {
            int ind = G[nod].back();
            G[nod].pop_back();

            if (!elim[ind])
            {
                elim[ind] = 1;

                int to;
                if (a[ind].x == nod) to = a[ind].y;
                else to = a[ind].x;

                v.push_back(to);
            }
        }
        else
        {
            v.pop_back();
            sol.push_back(nod);
        }
    }

    for (int i = 0; i < sol.size()-1; ++i)
        g << sol[i] << " ";
    return 0;
}