Pagini recente » Cod sursa (job #915050) | Cod sursa (job #1469571) | Cod sursa (job #418561) | Cod sursa (job #566701) | Cod sursa (job #2509320)
#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;
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 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)
swap(from, to);
st.push(to);
edges[last].vis = true;
}
}
int main() {
read();
if (isEulerian() == false) {
fout << -1;
return 0;
}
solve();
return 0;
}