Cod sursa(job #3311800)

Utilizator Alexbora13Bora Ioan Alexandru Alexbora13 Data 24 septembrie 2025 12:16:12
Problema Ciclu Eulerian Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;

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

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

struct str{
    int first;
    int second;
};

int n, m, x, y;
vector <str> graf[NMAX+1];
vector <int> st;
int grad[NMAX+1];
bool trecut[MMAX+1];
int ans[NMAX+1];

int main()
{
    fin >> n >> m;
    for(int i=1; i<=m; i++)
    {
        fin >> x >> y;
        graf[x].push_back( {y,i} );
        graf[y].push_back( {x,i} );
        grad[x]++; grad[y]++;
    }
    for(int i=1; i<=n; i++)
        if(grad[i] % 2)
        {
            fout << -1;
            return 0;
        }
    st.push_back(1);
    int cnt = 0;
    while(!st.empty())
    {
        bool gol = false;
        int nod = st.back();
        if(!graf[nod].empty())
        {
            str a  = graf[nod].back();
            if(!trecut[a.second])
            {
                st.push_back(a.first);
                trecut[a.second] = true;
            }
            graf[nod].pop_back();
        }
        else 
            gol = true;
        if(!st.empty() && gol == true)
            ans[++cnt] = st.back(), st.pop_back();
    }
    if(cnt-1 != m)
    {
        fout << -1;
        return 0;
    }
    for(int i=1; i<cnt; i++)
        fout << ans[i] << ' ';
    return 0;
}