Pagini recente » Cod sursa (job #2672958) | Cod sursa (job #693128) | Cod sursa (job #1260058) | Borderou de evaluare (job #1569416) | Cod sursa (job #3170444)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define nl '\n'
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 5*1e5+1;;
int n, m, fr[NMAX];
vector<int> graph[NMAX];
pair<int,int> edge[NMAX];
stack<int> st, cycle;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
fin >> u >> v;
edge[i] = {u, v};
graph[u].push_back(i);
graph[v].push_back(i);
}
bool flag = 1;
for (int i = 1; i <= n; i++)
if (graph[i].size()&1)
flag = 0;
if (!flag)
{
fout << -1;
return 0;
}
st.push(1);
while (!st.empty())
{
int nod = st.top();
if (graph[nod].size())
{
int i = graph[nod].back();
graph[nod].pop_back();
if (!fr[i])
{
fr[i] = 1;
int nx = edge[i].first;
if (nx == nod)
nx = edge[i].second;
st.push(nx);
}
}
else
{
st.pop();
cycle.push(nod);
}
}
while (cycle.size() > 1)
{
fout << cycle.top() << ' ';
cycle.pop();
}
return 0;
}