Pagini recente » Cod sursa (job #104709) | Cod sursa (job #3288801) | Cod sursa (job #2531208) | Cod sursa (job #39651) | Cod sursa (job #2509338)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int MAXN = 100010, MAXM = 500010;
struct Edge {
int from, to;
bool vis;
}edges[MAXM];
vector<int> graph[MAXN];
stack<int> st;
int gr[MAXN], k, n, m;
bool used[MAXN];
void read() {
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
graph[x].pb(k);
graph[y].pb(k);
edges[k++] = {x, y, false};
++gr[x];
++gr[y];
}
}
bool isEulerian() {
for (int i = 1; i <= n; ++i)
if (gr[i] & 1)
return 0;
return 1;
}
void dfs(int node) {
used[node] = true;
for (const auto &it: graph[node]) {
int from = edges[it].from, to = edges[it].to;
if (from != node)
to = from;
if (used[to] == false)
dfs(to);
}
}
bool isConex() {
for (int i = 1; i <= n; ++i)
if (gr[i]) {
dfs(i);
break;
}
for (int i = 1; i <= n; ++i)
if (used[i] == 0 && gr[i])
return false;
return true;
}
void solve() {
st.push(1);
while (!st.empty()) {
int node = st.top();
if (graph[node].size() == 0) {
st.pop();
if (!st.empty())
fout << node << ' ';
continue;
}
int last = graph[node].back();
int from = edges[last].from, to = edges[last].to;
bool vis = edges[last].vis;
graph[node].pop_back();
if (vis == true)
continue;
if (from != node)
to = from;
st.push(to);
edges[last].vis = true;
}
}
int main() {
read();
if (isEulerian() == false || isConex() == false) {
fout << -1;
return 0;
}
solve();
return 0;
}