Cod sursa(job #3281037)

Utilizator bogdan1479Luca Bogdan Alexandru bogdan1479 Data 28 februarie 2025 09:15:09
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <bitset>
#include <vector>
#include <stack>

using namespace std;

const int NMAX = 1e5 + 1, MMAX = 5e5 + 1;

struct muchie
{
    int nod, nrm;
};

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

int n, m;

vector<muchie> G[NMAX];
vector<int> L;
bitset<MMAX> elim;
stack<int> S;

void citire()
{
    int x, y;
    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});
    }
}

void euler()
{
    int x;
    muchie mm;
    S.push(1);
    while(!S.empty())
    {
        x = S.top();
        if(!G[x].empty())
        {
            mm = G[x].back();
            G[x].pop_back();
            if(!elim[mm.nrm])
            {
                elim[mm.nrm] = 1;
                S.push(mm.nod);
            }
        }
        else
        {
            L.push_back(x);
            S.pop();
        }
    }
}

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