Cod sursa(job #1758106)

Utilizator crushackPopescu Silviu crushack Data 16 septembrie 2016 16:16:04
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

typedef vector<list<int>> Graph;

void delete_edge(Graph& graph, int a, int b) {
  graph[a].erase(find(graph[a].begin(), graph[a].end(), b));
  graph[b].erase(find(graph[b].begin(), graph[b].end(), a));
}

void euler(Graph& graph, vector<int>& ans, int node) {
  while (!graph[node].empty()) {
    int adj = *graph[node].begin();
    delete_edge(graph, node, adj);
    euler(graph, ans, adj);
  }
  ans.push_back(node);
}

int main() {
  Graph graph;
  vector<int> ans;
  ifstream fin("ciclueuler.in");
  ofstream fout("ciclueuler.out");
  int n, m;

  fin >> n >> m;
  graph.resize(n + 1);
  for (int e = 0; e < m; ++e) {
    int x, y;
    fin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }

  euler(graph, ans, 1);

  if (n > 1) {
    ans.erase(ans.end() - 1);
  }

  for (int elem : ans) {
    fout << elem << " ";
  }
  fout << "\n";

  return 0;
}