Cod sursa(job #2608295)

Utilizator avtobusAvtobus avtobus Data 30 aprilie 2020 22:52:36
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;

ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
const int Mmax = 500555;

int main(void) {
  // freopen("ciclueuler.in", "r", stdin);
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int n, m, a, b;
  fin >> n >> m;
  vector<vi> G(n);
  vector<pii> edge(m);
  bitset<Mmax> used;
  rep(i, m) {
    fin >> a >> b;
    --a, --b;
    edge[i] = {a, b};
    G[a].push_back(i);
    G[b].push_back(i);
  }

  rep(i, n) {
    if (G[i].size() & 1) {
      fout << -1 << '\n';
      return 0;
    }
  }

  vi drum;
  stack<int> st;
  st.push(0);
  while(!st.empty()) {
    int x = st.top();
    while(!G[x].empty() && used[G[x].back()]) { G[x].pop_back(); }

    if (G[x].empty()) {
      st.pop();
      drum.push_back(x);
    } else {
      int e = G[x].back();
      used[e] = true;
      G[x].pop_back();

      int dest = edge[e].first ^ edge[e].second ^ x;
      st.push(dest);
    }
  }

  if (drum.size() < m+1) {
    fout << -1 << '\n';
  } else {
    rep(i, m) { fout << drum[i]+1 << ' '; }
    fout << '\n';
  }

  return 0;
}