Cod sursa(job #2575288)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 6 martie 2020 12:40:08
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;
const int MAXM = 500005;

struct Edge {
  int u, v;
  int other (const int node) {
    return u ^ v ^ node;
  }
}e[MAXM];

bool elim[MAXM];

vector<int>G[MAXN];
int gr[MAXN];
int cic[MAXM];
int k;

void euler(int node) {
  while (1) {
    while (gr[node] && elim[G[node][gr[node] - 1]])
      gr[node]--;
    if (gr[node] == 0)
      break;
    int w = G[node][gr[node] - 1];
    gr[node]--;
    elim[w] = 1;
    w = e[w].other(node);
    euler(w);
  }
  cic[++k] = node;
}

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) {
    scanf("%d%d", &e[i].u, &e[i].v);
    G[e[i].u].push_back(i);
    G[e[i].v].push_back(i);
    gr[e[i].u]++;
    gr[e[i].v]++;
  }

  for (int i = 1; i <= n; ++i)
    if (gr[i] % 2) {
      printf("-1\n");
      return 0;
    }

  euler(1);

  for (int i = 1; i <= m; ++i)
    printf("%d ", cic[i]);

  return 0;
}