Cod sursa(job #2574843)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 6 martie 2020 10:17:04
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <cstdio>
#include <vector>
#define NMAX 100000

using namespace std;

int n, m;
vector<int> st;
vector<int> v[NMAX + 5];

int main() {
  freopen("ciclueuler.in", "r", stdin);
  freopen("ciclueuler.out", "w", stdout);
  int x, y;

  scanf("%d %d", &n, &m);
  for(int i = 1; i <= m; i++) {
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }

  for(int i = 1; i <= n; i++)
    if(v[i].size() % 2 == 1){
      printf("-1");
      return 0;
    }

  st.push_back(1);
  while(st.size()) {
    x = st.back();
    if(v[x].size()) {
      y = v[x].back();
      st.push_back(y);

      v[x].pop_back();
      for(int i = 0; i < v[y].size(); i++)
        if(v[y][i] == x) {
          v[y].erase(v[y].begin() + i);
          break;
        }
    }
    else {
      if(st.size() > 1)
        printf("%d ", x);
      st.pop_back();
    }
  }

  return 0;
}