Cod sursa(job #2663301)

Utilizator ArkhamKnightyMarcus ArkhamKnighty Data 25 octombrie 2020 22:57:25
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <algorithm>
#include <fstream>
#include <vector>
#define SZ(x) ((int) (x).size())

using namespace std;

const int NMAX = 100005, MMAX = 500005;

ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");

vector<int> G[NMAX];
vector<int> ans;
vector<int> stk;

bool usedEdge[MMAX];
int from[MMAX], to[MMAX], n, m;

void citire()
{
    int x, y;
    cin >> n >> m;

    for (int i = 1; i <= m; ++i)
        cin >> x >> y,
            G[x].push_back(i),
            G[y].push_back(i),
            from[i] = x, to[i] = y;
}


int main()
{
    citire();
    for (int i = 1; i <= n; ++i)
    {
        if (SZ(G[i]) & 1)
        {
            cout << "-1\n";
            return 0;
        }
    }

    int tor;
    stk.push_back(1);
    while (!stk.empty())
    {
        int node = stk.back();
        if (!G[node].empty())
        {
            int e = G[node].back();
            G[node].pop_back();
            if (!usedEdge[e])

                usedEdge[e] = true,
                tor = from[e] ^ to[e] ^ node,
                stk.push_back(tor);

        }
        else
            stk.pop_back(),
            ans.push_back(node);

    }

    for (int i = 0; i < ans.size() - 1; ++i)
        cout << ans[i] << ' ';
    cout << '\n';

    cin.close();
    cout.close();

    return 0;
}