Cod sursa(job #1482501)

Utilizator hrazvanHarsan Razvan hrazvan Data 7 septembrie 2015 12:37:22
Problema Ciclu Eulerian Scor 50
Compilator c Status done
Runda Arhiva educationala Marime 1.18 kb
#include <stdio.h>
#define MAXN 100000
#define MAXM 500000
int ult[MAXN], deg[MAXN], nod[2 * MAXM], next[2 * MAXM];
int st[MAXM + 1], d = 0;
char fol[MAXM];

void ceuler(int x){
  int poz = ult[x], prev = -1;
  while(poz != -1){
    if(fol[poz / 2]){
      if(prev == -1)
        ult[x] = next[poz];
      else
        next[prev] = next[poz];
    }
    else{
      fol[poz / 2] = 1;
      ceuler(nod[poz]);
    }
    prev = poz;
    poz = next[poz];
  }
  st[d] = x + 1;
  d++;
}

int main(){
  FILE *in = fopen("ciclueuler.in", "r");
  int n, m, i, x, y, dr = 0;
  fscanf(in, "%d%d", &n, &m);
  for(i = 0; i < n; i++)
    ult[i] = -1;
  for(i = 0; i < m; i++){
    fscanf(in, "%d%d", &x, &y);
    x--;  y--;
    deg[x]++;
    deg[y]++;
    nod[dr] = x;
    next[dr] = ult[y];
    ult[y] = dr;
    dr++;
    nod[dr] = y;
    next[dr] = ult[x];
    ult[x] = dr;
    dr++;
  }
  fclose(in);
  FILE *out = fopen("ciclueuler.out", "w");
  char g = 0;
  for(i = 0; i < n; i++){
    if(deg[i] & 1)
      g = 1;
  }
  if(!g){
    ceuler(0);
    for(i = 0; i < d - 1; i++)
      fprintf(out, "%d ", st[i]);
  }
  else
    fprintf(out, "-1");
  return 0;
}