Cod sursa(job #2928518)

Utilizator YusyBossFares Yusuf YusyBoss Data 23 octombrie 2022 10:25:22
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>
#define NMAX 100000
#define MMAX 500000

using namespace std;

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

struct strson {
  int node, index;
};

vector <strson> vsons[NMAX + 1];
bool is_edge_used[MMAX + 1];
vector <int> vcycle;

bool check_grad(int n) {
  int node;

  node = 1;
  while (node <= n && vsons[node].size() % 2 == 0)
    node++;

  return node > n;
}

void dfs(int node) {
  int i;
  bool ok;
  int a;

  a = 2;

  while (!vsons[node].empty()) {
    int val = vsons[node].size();
    strson top = vsons[node].back();
    if (!is_edge_used[top.index]) {
      is_edge_used[top.index] = true;
      dfs(top.node);
    }

    if (!vsons[node].empty())
      vsons[node].pop_back();
  }

  vcycle.push_back(node);
}

int main() {
  int n, m, node1, node2, i;
  fin >> n >> m;

  for (i = 0; i < m; i++) {
    fin >> node1 >> node2;
    vsons[node1].push_back({node2, i});
    vsons[node2].push_back({node1, i});
  }

  if (check_grad(n)) {
    i = 1;
    while (i <= n && vsons[i].empty())
      i++;

    dfs(i);
    vcycle.pop_back();
    if (vcycle.size() == m) {
      for (i = 0; i < m; i++)
        fout << vcycle[i] << " ";
    }
    else
      fout << -1;
  }
  else
    fout << -1;
  return 0;
}