Pagini recente » Cod sursa (job #722876) | Cod sursa (job #2877306) | Cod sursa (job #1665189) | Cod sursa (job #2081566) | Cod sursa (job #2346089)
#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, k;
int sol[500005];
int f[500005];
vector <pair<int, int>> graph[100005];
void euler(int a)
{
stack <int> stk;
stk.push(1);
while (stk.size()) {
int node = stk.top();
if (!graph[node].size()) {
stk.pop();
sol[++k] = node;
continue;
}
int neighNode = graph[node].back().first;
int edge = graph[node].back().second;
graph[node].pop_back();
if (!f[edge]) {
f[edge] = 1;
stk.push(neighNode);
}
}
}
int main()
{
ios::sync_with_stdio(false);
fin.tie(0); fout.tie(0);
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
graph[x].push_back({ y, i });
graph[y].push_back({ x, i });
}
for (int i = 1; i <= n; ++i) {
if (graph[i].size() == 0 || graph[i].size() % 2) {
fout << -1 << '\n';
return 0;
}
}
euler(1);
for (int i = 1; i < k; ++i) fout << sol[i] << " ";
}