Pagini recente » Cod sursa (job #2751137) | Cod sursa (job #3142232) | Cod sursa (job #870432) | Cod sursa (job #144971) | Cod sursa (job #1866631)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int NMAX = 100000 + 5;
const int MMAX = 5 * NMAX;
struct Edge {
int a, b;
bool vis;
int other(int node) {
if (node == a)
return b;
else
return a;
}
} edges[MMAX];
int N, M;
vector <int> graph[NMAX];
stack <int> stk;
vector <int> sol;
int main()
{
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
cin >> N >> M;
for (int i = 1; i <= M; ++ i) {
cin >> edges[i].a >> edges[i].b;
graph[edges[i].a].push_back(i);
graph[edges[i].b].push_back(i);
}
bool bad = false;
for (int i = 1; i <= N; ++ i)
if (graph[i].size() % 2 == 1) {
bad = true;
break;
}
if (bad) {
cout << "-1\n";
return 0;
}
stk.push(1);
while (!stk.empty()) {
int node = stk.top();
bool found = false;
for (int i = 0; i < graph[node].size(); ++ i)
if (!edges[graph[node][i]].vis) {
edges[graph[node][i]].vis = true;
stk.push(edges[graph[node][i]].other(node));
found = true;
break;
}
if (!found) {
stk.pop();
sol.push_back(node);
}
}
reverse(sol.begin(), sol.end());
sol.pop_back();
for (int i = 0; i < sol.size(); ++ i)
cout << sol[i] << " \n"[i + 1 == sol.size()];
return 0;
}