Pagini recente » Istoria paginii runda/preoji2014_1/clasament | Istoria paginii runda/preoji2014_1/clasament | Istoria paginii runda/necartofeala_1/clasament | Istoria paginii runda/necartofeala_1/clasament | Cod sursa (job #3001473)
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>
#define NMAX 100005
#define MMAX 500005
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N, M;
vector<vector<pair<int, int>>> gr;
bool used[MMAX];
void read() {
fin >> N >> M;
gr.resize(N + 1);
int x, y;
for (int i = 0; i < M; ++i) {
fin >> x >> y;
gr[x].emplace_back(y, i);
gr[y].emplace_back(x, i);
}
}
bool isEuler() {
for (int i = 1; i <= N; ++i) {
if (gr[i].size() % 2 == 1) {
return false;
}
}
return true;
}
void euler(int nod) {
stack<int> st;
vector<int> answer;
st.push(nod);
while (!st.empty()) {
int top = st.top();
if (gr[top].size() == 0) {
answer.push_back(top);
st.pop();
}
else {
pair<int, int> edge = gr[top].back();
gr[top].pop_back();
if (!used[edge.second]) {
used[edge.second] = true;
st.push(edge.first);
}
}
}
for (int i = answer.size() - 1; i >= 1; --i) {
fout << answer[i] << " ";
}
fout << "\n";
}
int main()
{
read();
if (!isEuler()) {
fout << -1 << "\n";
}
else {
euler(1);
}
return 0;
}