Cod sursa(job #2175831)

Utilizator Victor24Vasiesiu Victor Victor24 Data 16 martie 2018 19:20:54
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

int n, a, b, m, nr, nod, rsp[500005], cnt;

ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");

vector < pair < int, int > > G[100005];

bitset <500005> fv;

stack < int > st;


void dfs ( int nod )
{
    fv[nod] = true;
    nr++;

    for( auto i : G[nod] )
    {
        if ( fv[ i.first ] == false )
        {
            dfs( i.first );
        }
    }

}


int main ()
{
    f>>n>>m;

    for ( int i = 1; i <= m ; i++ )
    {
        f>>a>>b;

        G[a].push_back( { b , i} );
        G[b].push_back( { a , i });
    }

    dfs(1);

    if ( nr != n )
    {
        g<<-1;
        return 0;
    }

    for ( int i = 1; i <= n ; i++ )
    {
        if ( G[i].size() % 2 == 1 )
        {
            g<<-1;
            return 0;
        }
    }

    fv.reset();

    st.push( 1 );

    while ( !st.empty() )
    {
        nod = st.top();

        if ( G[nod].size() == 0 )
        {
            rsp[++cnt] = nod;
            st.pop();
        }
        else
        {
            int muchie = G[nod].back().second;
            int cand = G[nod].back().first;

            G[nod].pop_back();

            if ( fv[muchie] == false )
            {
                fv[muchie] = true;
                st.push( cand );
            }

        }

    }

    for (int i = 1; i < cnt ; i++ )
    {
        g<<rsp[i]<<" ";
    }

}