Pagini recente » Cod sursa (job #3258750) | Cod sursa (job #1210283) | Cod sursa (job #2003712) | Monitorul de evaluare | Cod sursa (job #2092537)
#include <stdio.h>
#include <vector>
#include <stack>
const int MAX_N = 100000;
const int MAX_M = 500000;
struct Edge {
int u;
int poz;
};
int n, m, g[1 + MAX_N];
std::vector<Edge> neighbours[1 + MAX_N];
int answer[2 * MAX_M], nrs;
bool visited[1 + MAX_M];
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int p, q;
scanf("%d%d", &p, &q);
g[p]++;
g[q]++;
neighbours[p].push_back(Edge{q, i});
neighbours[q].push_back(Edge{p, i});
}
for (int i = 1; i <= n; i++)
if (g[i] % 2 != 0) {
printf("-1\n");
return 0;
}
std::stack <int> answer;
answer.push(1);
int nrsol = 0;
while (!answer.empty()) {
int last = answer.top();
if (!neighbours[last].empty()) {
if (visited[neighbours[last].back().poz] == false) {
answer.push(neighbours[last].back().u);
visited[neighbours[last].back().poz] = true;
}
neighbours[last].pop_back();
}
else {
nrsol++;
if (nrsol <= m)
printf("%d ", last);
answer.pop();
}
}
return 0;
}