Cod sursa(job #2855683)

Utilizator RTG123Razvan Diaconescu RTG123 Data 22 februarie 2022 19:17:07
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define MAXN 100001
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,x,y,ok,from[5*MAXN],to[5*MAXN],viz[5*MAXN];
vector <int> nodes;
vector <vector <int>> v(MAXN);
void exec (int cur,int counter)
{
    stack <int> st;
    st.push(cur);
    while (!st.empty())
    {
        int a=st.top();
        if (!v[a].empty())
        {
            int next=v[a].back();
            v[a].pop_back();
            if (!viz[next])
            {
                viz[next]=1;
                int go=to[next] ^ from[next] ^ a;
                st.push(go);
            }
        }
        else
        {
            st.pop();
            nodes.push_back(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 ok=0;
    for (int i=1; i<=n && ok==0; i++)
    {
        if (v[i].size() & 1)
        {
            ok=1;
            g<<-1;
        }
    }
    if (ok==0)
    {
    exec(1,0);

    for (int i=0; i<nodes.size()-1; i++)
        g<<nodes[i]<<' ';
    }
    return 0;
}