Pagini recente » Cod sursa (job #443698) | Cod sursa (job #171402) | Cod sursa (job #1389153) | Cod sursa (job #2219207) | Cod sursa (job #2647444)
#include <bits/stdc++.h>
#define newline '\n'
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
///***********************
const int NMAX = 1e5 + 3, MMAX = 5e5 + 3;
int n, m;
vector<pair<int, int>> graph[NMAX];//{node, nredge}
bool visEdge[MMAX];
int main() {
fin >> n >> m;
for (int i = 1, a, b; i <= m; i++) {
fin >> a >> b;
graph[a].push_back(make_pair(b, i));
graph[b].push_back(make_pair(a, i));
}
for (int i = 1; i <= n; i++)
if (graph[i].size() & 1)
return fout << -1 << newline, 0;
stack<int> st;
vector<int> ans;
st.push(1);
while (!st.empty()) {
int node = st.top(), to, edge;
if (!graph[node].empty()) {
tie(to, edge) = graph[node].back();
graph[node].pop_back();
if (!visEdge[edge]) {
visEdge[edge] = true;
st.push(to);
}
} else {
ans.push_back(node);
st.pop();
}
}
ans.pop_back();
for (auto it : ans)
fout << it << ' ';
fout << newline;
fout.close();
return 0;
}