Cod sursa(job #3000471)

Utilizator MihaiCostacheCostache Mihai MihaiCostache Data 12 martie 2023 15:06:06
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;

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

struct muchie
{
    int x, y;
    bool viz;
}muchii[500005];
vector <int> l[100001];
int st[500005], sol[500005], n, m, x, y, nr;
int main()
{
    fin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        fin>>x>>y;
        muchii[i]={x, y};
        l[x].push_back(i);
        l[y].push_back(i);
    }
    for(int i=1; i<=n; i++)
    {
        if(l[i].size()%2!=0)
        {
            fout<<"-1";
            return 0;
        }
    }
    int k=1;
    st[k]=1;
    while(k>0)
    {
        int nod=st[k];
        int ok=0;
        while(!l[nod].empty())
        {
            int mch=l[nod].back();
            l[nod].pop_back();
            if(muchii[mch].viz==0)
            {
                int vecin;
                if(muchii[mch].x!=nod)
                    vecin=muchii[mch].x;
                else
                    vecin=muchii[mch].y;
                st[++k]=vecin;
                muchii[mch].viz=1;
                ok=1;
                break;
            }
        }
        if(ok==0)
        {
            sol[++nr]=nod;
            k--;
        }
    }
    if(nr!=m+1)
    {
        fout<<"-1";
        return 0;
    }
    for(int i=1; i<nr; i++)
    {
        fout<<sol[i]<<" ";
    }
    return 0;
}