Cod sursa(job #3260390)

Utilizator bogdan1479Luca Bogdan Alexandru bogdan1479 Data 2 decembrie 2024 09:17:07
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 1e5 + 1;

struct muchie
{
    int x, y;
};

int n, m;

vector<int> G[NMAX], L;
vector<muchie> M;
vector<bool> elim;
stack<int> S;

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

void citire()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        fin >> 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);
    }
}

void euler()
{
    int v, p, k;
    S.push(1);
    while(!S.empty())
    {
        v = S.top();
        if(!G[v].empty())
        {
            k = G[v].back();
            G[v].pop_back();
            if(!elim[k])
            {
                elim[k] = 1;
                S.push(M[k].x + M[k].y - v);
            }
        }
        else
        {
            L.push_back(v);
            S.pop();
        }
    }
}

int main()
{
    bool ok = 1;
    citire();
    for(int i = 1; i <= n && ok; ++i)
        if(G[i].size() & 1)
            ok = 0;
    if(ok)
    {
        euler();
        for(int i = L.size() - 1; i >= 1; --i)
            fout << L[i] << ' ';
    }
    else
        fout << "-1";
    return 0;
}