Cod sursa(job #2541335)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 8 februarie 2020 12:37:10
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#define NMAX 100000

using namespace std;

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

void dfs(int x, int ok) {
  int y;

  while(!v[x].empty()) {
    y = v[x].back();
    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;
      }

    dfs(y, 0);
  }

  if(!ok)
    printf("%d ", x);
}

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;
    }

  dfs(1, 1);

  return 0;
}