Pagini recente » Cod sursa (job #1487618) | Cod sursa (job #3304761) | Cod sursa (job #2224588) | Cod sursa (job #2604737) | Cod sursa (job #3324452)
#include <iostream>
#include <vector>
#include <stack>
const int NMAX = 100001;
const int MMAX = 500001;
int to[MMAX];
int from[MMAX];
bool used[MMAX];
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>>g;
g.resize(n + 1);
for(int i = 1; i <= m; i++)
{
int x, y;
std::cin >> x >> y;
g[x].push_back(i);
g[y].push_back(i);
from[i] = x;
to[i] = y;
}
for(int i = 1; i <= n; ++i)
{
if(g[i].size() % 2 == 1){
std::cout << -1 << '\n';
return 0;
}
}
std::stack<int>st;
st.push(1);
//incepem din nodul 1 accesam fiecare muchie
std::vector<int>ans;
while(!st.empty())
{
int node = st.top();
if(!g[node].empty())
{
int muchie = g[node].back();
g[node].pop_back();
if(!used[muchie])
{
used[muchie] = true;
if(node == from[muchie])
{
st.push(to[muchie]);
}
else st.push(from[muchie]);
}
}
else {
st.pop();
ans.push_back(node);
}
}
for(size_t i = 0; i < ans.size() - 1; ++i)
std::cout << ans[i] << ' ';
return 0;
}