Cod sursa(job #2239622)

Utilizator Y.MalmsteenB.P.M. Y.Malmsteen Data 11 septembrie 2018 13:00:07
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 100000;

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

int n, m;

vector<int> G[NMAX + 1], L;
vector<pair<int, int>> M;
vector<bool> elim;

stack<int> S;

void euler()
{
    S.push(1);
    while(!S.empty())
    {
        int v = S.top();
        if(G[v].size() > 0)
        {
            int x = G[v].back();
            G[v].pop_back();
            if(!elim[x])
            {
                elim[x] = 1;
                int p = M[x].second;
                if(p == v)
                    p = M[x].first;
                S.push(p);
            }
        }
        else
        {
            L.push_back(v);
            S.pop();
        }
    }
}

int main()
{
    int x, y;
    f >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        f >> x >> y;
        M.push_back({x, y});
        elim.push_back(0);
        G[x].push_back(M.size() - 1);
        G[y].push_back(M.size() - 1);
    }
    for(int i = 1; i <= n; i++)
        if(G[i].size() & 1)
        {
            g << "-1\n";
            return 0;
        }
    euler();
    for(unsigned int i = 0; i < L.size() - 1; i++)
        g << L[i] << ' ';
    return 0;
}