Cod sursa(job #1307019)

Utilizator thinkphpAdrian Statescu thinkphp Data 31 decembrie 2014 20:11:33
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define FIN "ciclueuler.in"
#define FOUT "ciclueuler.out"
#define NMAX 100005
#define MMAX 500005

using namespace std;

vector<int> G[ NMAX ];
stack<int> STACK;

int degree[ NMAX ],
    sol[ NMAX ],
    num_nodes, num_edges,
    x,y,a,b;
 
int main() {

    freopen(FIN, "r", stdin);

    freopen(FOUT, "w", stdout);

    scanf("%d %d", &num_nodes, &num_edges);

    for(int i = 1; i <= num_edges; i++) {

          scanf("%d %d", &x, &y);

          G[ x ].push_back( y );  
          G[ y ].push_back( x );
 
          degree[ x ]++;
          degree[ y ]++;
    }     

    fclose( stdin ); 

    STACK.push( 1 );

    while(!STACK.empty()) {

           a = STACK.top();

           if(!degree[ a ]) {

               sol[++sol[0]] = a;
               STACK.pop();
               continue;  
           }

           b = G[ a ].back();

           G[ a ].pop_back();           

           degree[ a ]--;

           degree[ b ]--;

           STACK.push( b ); 

           for(vector<int>::iterator it = G[ b ].begin(); it != G[ b ].end(); ++it) {

               if(*it == a) {

                   G[ b ].erase( it );
                   break;
               }   
           } 
    }

    for(int i = 1; i <= num_edges; i++) printf("%d ", sol[ i ]); 

    fclose( stdout );

    return(0);
};