Cod sursa(job #2864821)

Utilizator vladp1324Vlad Pasare vladp1324 Data 8 martie 2022 11:40:27
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");

int n, m, from[500001], to[500001];
bool ver[500001];
vector < int > v[100001], r;
stack < int > st;

int main()
{
  fin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    fin >> x >> y;
    from[i] = x;
    to[i] = y;
    v[x].push_back (i);
    v[y].push_back (i);
  }
  for (int i = 1; i <= n; i++)
    if (v[i].size () % 2 == 1) {
      fout << -1;
      return 0;
    }
  st.push (1);
  while (not st.empty ()) {
    int nc = st.top ();
    if (v[nc].size ()) {
      int mc = v[nc].back ();
      v[nc].pop_back ();
      if (not ver[mc]) {
        ver[mc] = true;
        int nv = (nc ^ from[mc] ^ to[mc]);
        st.push (nv);
      }
    }
    else {
      r.push_back (nc);
      st.pop ();
    }
  }
  r.pop_back ();
  for (int i = 0; i < r.size (); i++)
    fout << r[i] << ' ';
  return 0;
}