Cod sursa(job #3359412)

Utilizator Dani111Gheorghe Daniel Dani111 Data 27 iunie 2026 20:08:10
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 100001;
const int Mmax = 500001;

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

int n, m;

vector<pair<int, int>> graf[Nmax];
bitset<Mmax> seenMuchie;
bitset<Nmax> seenNod;

bool isEulerian = true;

vector<int> eulerianCycle;
void dfs(int nod)
{
    //cout << nod << " ";

    seenNod[nod] = 1;

    if((int)(graf[nod].size()) % 2 == 1)
        isEulerian = false;

    if(isEulerian)
    {
        //cout << "lol";
        for(auto x : graf[nod])
        {
            if(!seenMuchie[x.second])
            {
                seenMuchie[x.second] = 1;
                dfs(x.first);
            }
        }
        eulerianCycle.push_back(nod);
    }

}

int main()
{
    fin >> n >> m;
    int x, y;
    for(int i = 1; i <= m; ++i)
    {
        fin >> x >> y;
        graf[x].push_back({y, i});
        graf[y].push_back({x, i});
    }

    dfs(1);

    for(int i = 1; i <= n; ++i)
        if(seenNod[i] == 0)
            isEulerian = false;

    if(isEulerian)
    {
        for(int i = 0; i < (int)eulerianCycle.size() - 1; ++i)
            fout << eulerianCycle[i] << " ";
    }
    else
        fout << "-1";

    return 0;
}