Cod sursa(job #2173966)

Utilizator chioreanraulChiorean Raul chioreanraul Data 16 martie 2018 10:01:53
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <vector>
#include <stack>

#define nod first
#define indice second

using namespace std;
int n,i,j,m,fv[100005],rsp,x,y,lastind[100005],nodul,lung;

vector < pair < int, int > >v[100005];
stack < int > s;
pair < int, int > aux;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
void vconex( int nod )
{
    fv[ nod ] = 1;
    for( int k = 0 ; k < v[ nod ].size(); k++ )
    {
        if( fv[ v[ nod ][ k ].nod ] == 0 )
        vconex( v[ nod ][ k ].nod);
    }
}
int main()
{
    fin>>n>>m,rsp = 1;
    for( i = 1 ; i <= m; i++ )
    {
        fin>>x>>y;
        v[ x ].push_back( { y , i } );
        v[ y ].push_back( { x , i } );
        fv[ x ]++;
        fv[ y ]++;
    }
    for( i = 1; i <= n; i++ )
        if( fv[ i ] % 2 == 1)
            rsp = 0;
    for( i = 1; i <= n; i++ )
        fv[ i ] = 0;
    vconex( 1 );
    for( i = 1; i <= n; i++ )
    {
        rsp = rsp * fv[ i ];
    }
    if( rsp == 1 )
    {
        for( i = 1; i <= n; i++ )
            fv[ i ] = 0;
        s.push( 1 );
        while( !s.empty())
        {
           nodul = s.top( );
           lung = v[ nodul ].size( );
           while( lastind[ nodul ] < lung )
           {
               aux = v[ nodul ][ lastind[ nodul ] ];
               if( fv[ aux.second ] == 0)
               {
                   fv[ aux.second ] = 1;
                   s.push( aux.nod );
                   break;
               }
               lastind[ nodul ]++;
           }
           if( lastind[ nodul ] == lung )
           {
               fout<<nodul<<" ";
               s.pop( );
           }
        }


    }
    return 0;
}