Pagini recente » Cod sursa (job #2254567) | Cod sursa (job #2143643) | Istoria paginii runda/simulare_03_04_2019/clasament | Cod sursa (job #1189828) | Cod sursa (job #2029241)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define lim 500010
int n,m,k,st[lim];
vector <int> G[lim];
vector <int> ans;
void sterg (int X, int Y)
{
for (vector < int > :: iterator it = G[X].begin(); it != G[X].end(); it ++)
if (*it == Y)
{
G[X].erase (it);
return;
}
}
int main()
{
int x,y;
fin>>n>>m;
for (int i=1; i<=m; i++)
{
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for (int i=1; i<=n; i++)
if (G[i].size() % 2)
{
fout<<-1;
return 0;
}
k=1;
st[k]=1;
while (k)
{
if (G[st[k]].empty())
ans.push_back(st[k--]);
else
{
st[++k] = G[st[k-1]][0];
G[st[k-1]].erase(G[st[k-1]].begin());
sterg (st[k], st[k-1]);
}
}
for (auto it:ans)
fout<<it<<' ';
fin.close();
fout.close();
return 0;
}