Pagini recente » Cod sursa (job #2120594) | Cod sursa (job #2282060) | Cod sursa (job #2403311) | Cod sursa (job #2580778) | Cod sursa (job #2631102)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 1e5+5;
const int MMAX = 5e5+5;
int n,m;
bool v[MMAX];
vector <pair<int ,int> >a[NMAX];
stack <int> st;
int main()
{
fin >> n >> m;
for(int i=1;i<=m;i++)
{
int x,y;
fin >> x >> y;
a[x].push_back({y,i});
a[y].push_back({x,i});
}
for(int i=1;i<=n;i++)
if(a[i].size()%2)
{
fout << -1;
return 0;
}
st.push(1);
while(st.size())
{
int nod=st.top();
while(a[nod].size() and v[a[nod].back().second])
a[nod].pop_back();
if(a[nod].empty())
{
fout << nod << " ";
st.pop();
continue;
}
st.push(a[nod].back().first);
v[a[nod].back().second]=true;
a[nod].pop_back();
}
return 0;
}