Pagini recente » Cod sursa (job #2815276) | Cod sursa (job #3200287) | Cod sursa (job #2871842) | Cod sursa (job #809395) | Cod sursa (job #2715167)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
struct Edge
{
int dest, poz;
};
bool viz[500005];
vector <Edge> v[100005];
int main()
{
int N, M;
fin >> N >> M;
for(int i = 1; i <= M; i++)
{
int x, y;
fin >> x >> y;
v[x].push_back({y, i});
v[y].push_back({x, i});
}
for(int i = 1; i <= N; i++)
if(v[i].size() % 2 == 1)
{
fout << -1 << '\n';
return 0;
}
vector <int> ans;
stack <int> st;
st.push(1);
while(!st.empty())
{
int node = st.top();
if(v[node].size() == 0)
{
st.pop();
ans.push_back(node);
continue;
}
int nextt = (*v[node].begin()).dest;
int poz = (*v[node].begin()).poz;
v[node].erase(v[node].begin());
if(viz[poz])
continue;
viz[poz] = true;
st.push(nextt);
}
reverse(ans.begin(), ans.end());
for(int i = 0; i < ans.size() - 1; i++)
fout << ans[i] << " ";
return 0;
}