Pagini recente » Cod sursa (job #1996146) | Cod sursa (job #991878) | Cod sursa (job #436192) | Cod sursa (job #805040) | Cod sursa (job #1921145)
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
bool IsEulerian(const Graph &g)
{
for (const auto &node : g) {
if (node.size() % 2 != 0) {
return false;
}
}
return true;
}
void Dfs(Graph &g, int node, vector<int> &cycle)
{
while (!g[node].empty()) {
int next = g[node].back();
g[node].pop_back();
g[next].erase(find(g[next].begin(), g[next].end(), node));
Dfs(g, next, cycle);
}
cycle.push_back(node);
}
vector<int> FindCycle(Graph &g)
{
vector<int> cycle;
Dfs(g, 0, cycle);
cycle.pop_back();
return cycle;
}
int main()
{
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
fin >> n >> m;
Graph graph(n);
while (m--) {
int x, y;
fin >> x >> y;
graph[x - 1].push_back(y - 1);
graph[y - 1].push_back(x - 1);
}
if (!IsEulerian(graph)) {
fout << "-1\n";
return 0;
}
auto cycle = FindCycle(graph);
for (int node : cycle) {
fout << node + 1 << " ";
}
return 0;
}