Cod sursa(job #2869471)

Utilizator RTG123Razvan Diaconescu RTG123 Data 11 martie 2022 15:49:28
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define MAXN 100003
#define MAXM 500003
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,x,y,to[MAXM],nr,from[MAXM],muchiesused[MAXM],afis[MAXM*2];
vector <vector <int>> v(MAXN);
void dfs (int start)
{
    stack <int> st;
    st.push(start);
    while (!st.empty())
    {
        int a=st.top();
        if (!v[a].empty())
        {
            //if (viz[a]==0)
            //{
                //viz[a]=1;
            int nextnod=v[a].back(),next=0;
            v[a].pop_back();
            if (muchiesused[nextnod]==0)
            {
                muchiesused[nextnod]=1;
            if (from[nextnod]==a)
            {
                next=to[nextnod];
            }
            else next=from[nextnod];
            st.push(next);
            }
            //}
        }
        else
        {
            st.pop();
            nr++;
            afis[nr]=a;
        }
    }
}
int main()
{
    f>>n>>m;
    for (int i=1; i<=m; i++)
    {
        f>>x>>y;
        v[x].push_back(i);
        v[y].push_back(i);
        from[i]=x;
        to[i]=y;
    }
    int stop=0;
    for (int i=1; i<=n && !stop; i++)
   {
       if (v[i].size() & 1)
        stop=1;
   }
   if (stop==0)
   {
    //cout<<v[7759].size()<<'\n';
    dfs(1);
   // cout<<nr;
  // cout<<afis[nr-1];
   // cout<<afis[nr]<<'\n';
    for (int i=1; i<nr; i++)
        g<<afis[i]<<' ';
   }
   else g<<-1;
    return 0;
}