Pagini recente » Cod sursa (job #2986211) | Cod sursa (job #1447417) | Cod sursa (job #1182278) | Cod sursa (job #2560938) | Cod sursa (job #2837313)
#include <iostream>
#include <vector>
#include <stack>
#define NMAX 100005
#define MMAX 500005
using namespace std;
struct muchie {
int x, y;
bool used;
} a[MMAX];
int n, m;
vector<int> adj[NMAX], ans;
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1, x, y; i <= m; ++i) {
scanf("%d%d", &x, &y);
a[i] = {x, y, 0};
adj[x].push_back(i);
adj[y].push_back(i);
}
for(int i = 1; i <= n; ++i) {
if(adj[i].size() % 2) {
printf("-1");
return 0;
}
}
stack<int> st;
st.push(1);
while(!st.empty()) {
int nod = st.top();
if(!adj[nod].empty()) {
int muchie = adj[nod].back();
adj[nod].pop_back();
if(!a[muchie].used) {
a[muchie].used = 1;
st.push(a[muchie].x ^ a[muchie].y ^ nod);
}
} else {
ans.push_back(nod);
st.pop();
}
}
for(int i = 0; i < ans.size() - 1; ++i)
printf("%d ", ans[i]);
return 0;
}