Pagini recente » Cod sursa (job #2829542) | Cod sursa (job #3274611) | Cod sursa (job #2662933) | Cod sursa (job #1154222) | Cod sursa (job #2256264)
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <assert.h>
#include <queue>
#include <chrono>
#include <memory>
#include <cstring>
using LL = long long;
using ULL = int long long;
const std::string _problemName = "eulerian";
#define USE_FILES
#ifdef USE_FILES
namespace std {
std::ifstream fin(_problemName + ".in");
std::ofstream fout(_problemName + ".out");
}
#define cin fin
#define cout fout
#endif
using Edge = int;
using Graph = std::vector< std::vector<Edge> >;
bool hasEulerianCycle(const Graph& graph) {
for (const auto& nodeNeighbours : graph) {
if (nodeNeighbours.size() % 2 != 0) {
return false;
}
}
return true;
}
void addEdge(Graph& graph, int x, int y) {
graph[x].push_back(y);
graph[y].push_back(x);
}
std::vector<int> eulerianCycle;
Graph graph;
bool exists(const Edge& edge) {
return edge != -1;
}
int getNeighbour(const Edge& edge) {
return edge;
}
void remove(const Edge& edge) {
}
void dfs(int node) {
bool hasNeighbour = false;
for (auto& edge : graph[node]) {
if (exists(edge)) {
int neighbour = getNeighbour(edge);
edge = -1;
graph[neighbour]
auto it = std::find(graph[neighbour].begin(), graph[neighbour].end(), node);
*it = -1;
dfs(neighbour);
hasNeighbour = true;
eulerianCycle.push_back(node);
}
}
if (!hasNeighbour) {
eulerianCycle.push_back(node);
}
}
std::vector<int> buildEulerianCycle() {
dfs(0);
}
int main() {
int n, m;
std::cin >> n >> m;
graph.resize(n);
while (m--) {
int x, y;
std::cin >> x >> y;
--x, --y;
addEdge(graph, x, y);
}
if (!hasEulerianCycle(graph)) {
std::cout << "-1\n";
return 0;
}
std::vector<int> eulerianCycle = buildEulerianCycle(graph);
for (const auto node : eulerianCycle) {
std::cout << node - 1 << ' ';
}
std::cout << '\n';
return 0;
}