Pagini recente » Cod sursa (job #296772) | Cod sursa (job #1310656) | Cod sursa (job #2273295) | Cod sursa (job #2432580) | Cod sursa (job #2509372)
#include <bits/stdc++.h>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct muchie {
int from, to;
bool vis;
} muchii[500010];
vector<int> graph[100010];
stack<int> st;
int gr[100010], k, n, m;
void citire() {
f >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
f >> x >> y;
graph[x].push_back(k);
graph[y].push_back(k);
muchii[k++] = {x, y, false};
gr[x]++;
gr[y]++;
}
}
bool iiEulerian() {
for (int i = 1; i <= n; i++)
if (gr[i] & 1)
return 0;
return 1;
}
void rezolva() {
st.push(0);
while (!st.empty()) {
int nod = st.top();
if (graph[nod].empty()) {
st.pop();
if (!st.empty())
g << nod << ' ';
continue;
}
int last = graph[nod].back();
int from = muchii[last].from, to = muchii[last].to;
bool vis = muchii[last].vis;
graph[nod].pop_back();
if (vis)
continue;
if (from != nod)
swap(from, to);
st.push(to);
muchii[last].vis = true;
}
}
int main() {
citire();
if (!iiEulerian()) {
g << -1;
return 0;
}
rezolva();
return 0;
}