Cod sursa(job #2696765)

Utilizator RomanacheRoman Alexandru-George Romanache Data 16 ianuarie 2021 16:21:04
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

fstream f("ciclueuler.in",ios::in);
fstream g("ciclueuler.out",ios::out);

struct ab
{
    int a, b;
};

const int nmax = 1e5+5, mmax = 5e5+5;

int n,m,a,b,sol;

int frecv[nmax], termin[mmax];

vector <ab> nod[nmax];

vector <int> st, stans;

void euler(int curent)
{
    st.push_back(curent);

    int a, b;

    while(!st.empty())
    {
        a = st.back();
        if(!nod[a].empty())
        {
            b = nod[a].back().b;
            if(termin[b] == false)
            {
                termin[b] = true;
                st.push_back(nod[a].back().a);
            }
            nod[a].pop_back();
        }
        else
        {
            st.pop_back();
            stans.push_back(a);
        }
    }

    for(int i=1;i<=stans.size()-1;i++)
    {
        g<<stans[i-1]<<' ';
    }

}


int main()
{
    int i;
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>a>>b;
        nod[a].push_back({b, i});
        nod[b].push_back({a, i});
        frecv[a]++;
        frecv[b]++;
    }
    for(i=1;i<=n;i++)
    {
        if(frecv[i]%2)
        {
            g<<-1;
            return 0;
        }
    }
    euler(1);
    f.close();
    g.close();

    return 0;
}