Cod sursa(job #2371993)

Utilizator BarbumateiBarbu Matei Barbumatei Data 6 martie 2019 20:45:10
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <cstdio>
#include <vector>
#define MAXN 100000
#define MAXM 500000
using namespace std;
FILE *fin, *fout;
vector <int> G[MAXN];
int stiva[MAXM + 1], nr_s, coada[MAXM + 1], nr_c;
bool seen[MAXM];
int from[MAXM], to[MAXM];

int main() {
  int n, m, i, x, y;
  fin = fopen( "ciclueuler.in", "r" );
  fout = fopen( "ciclueuler.out", "w" );
  fscanf( fin, "%d%d", &n, &m );
  for ( i = 0; i < m; i++ ) {
    fscanf( fin, "%d%d", &x, &y );
    x--; y--;
    G[x].push_back( i );
    G[y].push_back( i );
    from[i] = x;
    to[i] = y;
  }
  bool ok = true;
  i = 0;
  while ( i < n && ok ) {
    if ( G[i].size() & 1 )
      ok = false;
    i++;
  }
  stiva[nr_s] = 0;
  if ( ok )
    nr_s = 1;
  while ( nr_s ) {
    int nod = stiva[nr_s - 1];
    if ( !G[nod].empty() ) {
      i = G[nod].back();
      G[nod].pop_back();
      if ( seen[i] == false ) {
        seen[i] = true;
        stiva[nr_s++] = to[i] ^ from[i] ^ nod;
      }
    }
    else{
      coada[nr_c++] = nod;
      nr_s--;
    }
  }
  for ( i = 0; i < nr_c - 1; i++ )
    fprintf( fout, "%d ", coada[i] + 1 );
  if ( ok == false )
    fprintf( fout, "-1\n" );
  fclose( fin );
  fclose( fout );
    return 0;
}