Cod sursa(job #2232381)

Utilizator mouse_wirelessMouse Wireless mouse_wireless Data 18 august 2018 21:45:47
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp make_pair
#define CHECK(x) if(!(x)) return false;
#define CHECKRET(x, y) if(!(x)) return (y);
#define SKIP(x) if((x)) continue;
typedef pair<int, int> pii;

#ifdef INFOARENA
#define ProblemName "ciclueuler"
#endif

#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "fis.in"
#define OuFile "fis.out"
#endif

const int MAXN = 100010;
const int MAXM = 500010;
stack<int> G[MAXN];
vector<int> ciclu;
int to[MAXM * 2];
bool done[MAXM * 2];

void DFS(int x) {
  while (!G[x].empty()) {
    int iy = G[x].top(); G[x].pop();
    SKIP(done[iy]);
    done[iy] = done[iy ^ 1] = true;
    DFS(to[iy]);
  }
  ciclu.push_back(x);
}

int main() {
  assert(freopen(InFile, "r", stdin));
  assert(freopen(OuFile, "w", stdout));
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x, --y;
    to[2* i] = y; G[x].push(2 * i);
    to[2 * i + 1] = x; G[y].push(2 * i + 1);
  }
  DFS(0);
  ciclu.pop_back();
  if (ciclu.size() != M)
    puts("-1");
  else {
    for (const auto &it : ciclu)
      printf("%d ", it + 1);
    puts("");
  }
  return 0;
}