Cod sursa(job #2077368)

Utilizator TherevengerkingSurani Adrian Therevengerking Data 27 noiembrie 2017 22:37:57
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;
const int Nmax = 100000 + 5;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define pii pair<int, int>
#define x first
#define pb push_back
#define y second
vector<pii>v[Nmax];
stack<int>st;
bool viz[Nmax], fol[5 * Nmax];
int n, m, g[Nmax];
void dfs(int nod)
{
    viz[nod] = 1;
    for(auto i : v[nod])
        if(!viz[i.x])dfs(i.x);
}
int main()
{
    fin >> n >> m;
    for(int i =1, a, b; i <= m; ++i)
    {
        fin >> a >> b;
        v[a].pb({b, i});
        v[b].pb({a, i});
        g[a]++; g[b]++;
    }
    dfs(1);

    for(int i =1 ; i <= n; ++i)
        if(g[i] % 2 == 1 || !viz[i])
        {
            fout << -1;
            return 0;
        }
    st.push(1); int first = 0;
    while(!st.empty())
    {
        int nod = st.top();

        if(g[nod] == 0)
        {
            if(!first)first = 1;
            else fout << nod << ' ';
            st.pop();
            continue;
        }
        while(fol[v[nod].back().y])
            v[nod].pop_back();
        int fiu = v[nod].back().x;
        g[nod]--;g[fiu]--;
        fol[v[nod].back().y] = 1;
        v[nod].pop_back();
        st.push(fiu);

    }
    return 0;
}