Cod sursa(job #3376315)

Utilizator VladStroicaStroica Vlad Cristian VladStroica Data 17 noiembrie 2026 12:07:01
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <bits/stdc++.h>

using namespace std;
const int NMAX=1e5+5;
vector<int>lstrez;
vector<int>lst[NMAX];
int grd[NMAX];
struct cows
{
    int x,y;
};
cows mch[NMAX*5];
bool vf[NMAX];
bool vf2[NMAX*5];
int cont=0;
void Dfs(int poz)
{
    cont++;
    vf[poz]=true;
    for(auto i:lst[poz])
    {
        int i2;
        if(mch[i].x!=poz)
            i2=mch[i].x;
        else
            i2=mch[i].y;
        if(vf[i2]==false)
        {
            Dfs(i2);
        }
    }
}
void Dfs2(int poz)
{
    //int r=0;
    while(lst[poz].empty() == false)
    {
        int y=lst[poz].back();
        int i2;
        if(mch[y].x!=poz)
            i2=mch[y].x;
        else
            i2=mch[y].y;
        //swap(lst[poz][r],lst[poz].back());
        lst[poz].pop_back();
        if(vf2[y]==false)
        {
            vf2[y]=true;
            Dfs2(i2);
        }
        //r++;
    }
    lstrez.push_back(poz);
}
int main()
{
    ifstream cin("ciclueuler.in");
    ofstream cout("ciclueuler.out");
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        cin>>mch[i].x>>mch[i].y;
        lst[mch[i].x].push_back(i);
        lst[mch[i].y].push_back(i);
        grd[mch[i].x]++;
        grd[mch[i].y]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(grd[i]%2!=0)
        {
            cout<<-1;
            return 0;
        }
    }
    Dfs(1);
    if(cont!=n)
    {
        cout<<-1;
        return 0;
    }
    Dfs2(1);
    reverse(lstrez.begin(),lstrez.end());
    lstrez.pop_back();
    for(auto i:lstrez)
        cout<<i<<" ";
    return 0;
}