Cod sursa(job #2278494)

Utilizator vladisimovlad coneschi vladisimo Data 8 noiembrie 2018 09:09:57
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <cstdio>
#include <stack>
#include <vector>

const int MAX_N = 1e5, MAX_M = 5e5;

std::vector<int> edgeId[2 + MAX_N];

int from[2 + MAX_M], to[2 + MAX_M];

bool erased[2 + MAX_M];

int main() {
  freopen("ciclueuler.in", "r", stdin);
  freopen("ciclueuler.out", "w", stdout);
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    edgeId[u].push_back(i);
    edgeId[v].push_back(i);
    from[i] = u;
    to[i] = v;
  }
  std::stack<int> nodes;
  std::vector<int> sol;
  nodes.push(1);
  while (!nodes.empty()) {
    int u = nodes.top();
    if (!edgeId[u].empty()) {
      int e = edgeId[u].back();
      edgeId[u].pop_back();
      if (!erased[e]) {
        erased[e] = true;
        nodes.push(from[e] + to[e] - u);
      }
    } else {
      nodes.pop();
      sol.push_back(u);
    }
  }
  sol.pop_back();
  for (int i : sol)
    printf("%d ", i);
  return 0;
}