Cod sursa(job #1424469)

Utilizator CalinCojoFMI Cojocaru Calin George CalinCojo Data 24 aprilie 2015 15:43:49
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.03 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define MAX 100000

using namespace std;

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

vector < int > arc [ MAX ]  ;

int viz [ MAX ] ;
unsigned int n;

void sterge_muchie( unsigned int x, unsigned int y )
{
    unsigned int i;
    arc [ x ].pop_back();

    for ( i = 0 ; i < arc [ y ].size() ; i++)
            if ( arc [ y ] [ i ] == x )
            {
                arc [ y ].erase ( arc [ y ].begin()  + i  ) ;
                break;
            }

}


bool euler ( int node)
{
        unsigned int i, top_node ;
        bool FOUND  =  0, HAS_UNVISITED ;
        stack  < unsigned int > stiva ;
        stiva.push ( node );

        while ( !stiva.empty()  && !FOUND )
        {
            HAS_UNVISITED = 0;
            top_node = stiva.top();

            if ( !arc [ top_node].empty() )
            {
                HAS_UNVISITED = 1;
                i = arc [top_node].back();  ;
                stiva.push ( i );
                sterge_muchie ( top_node, i ) ;
            }
            else
            {
                if ( top_node == node)
                {
                    while ( !stiva.empty() )
                    {
                        g<<stiva.top()<<"  " ;
                        stiva.pop();
                    }
                    return 1;

                }
                else
                {
                    stiva.pop();
                }

            }

        }

}

bool dfs(){

    int top_node,node = 1 ;
    unsigned int i  ;
    stack <unsigned int > st ;
    bool contor ;

    st.push ( node );
    viz [ node ] = 1;

    while ( ! st.empty() ){

        contor = 0;
        top_node = st.top();
        for ( i = 0 ; i < arc [ top_node ].size() ; i++)
            if ( viz [ arc [ top_node ] [ i ] ] == 0 )
            {
                contor = 1;
                st.push ( arc [ top_node ] [ i ] ) ;
                viz [ arc [ top_node ] [ i ] ] = 1;
                break ;
            }

        //daca nodul nu mai are niciun vecin nevizitat facem pop
        if(!contor)
            st.pop();
    }


    // daca a ramas un nod nevizitat atunci graful nu e conex
    for ( i = 1; i <= n ; i++)
        if( viz[ i ] == 0 )
            return 0;

    return 1 ;



}

bool isEulerian ( ){

    unsigned int i ;
    // paritatea gradelor
    for ( i = 1 ; i <= n ; i++ )
        if ( arc [ i ].size() % 2 )
            return 0 ;

    //conexitate
    return dfs(  ) ;


}




int main()
{
   unsigned int x,y ,m,i;


    f>>n>>m;
    while ( m ){
        m--;
        f>>x>>y;
        arc [ x ].push_back ( y );
        arc [ y ]. push_back ( x );
    }

    /*ca sa admita un ciclu eurelian graful trebuie sa fie conex iar gradele tuturor nodurilor sa fie pare */
    if( isEulerian())
    {
            for ( i = 1 ; i <= n ; i++)
                viz [ i ] = 0;

            for ( i = 1 ; i <= n ; i++)
                if( euler ( i ) )
                    break;

    }
    else
        g<<"-1";



    return 0;
}