Pagini recente » Cod sursa (job #240371) | Cod sursa (job #1196378) | Cod sursa (job #1969343) | Cod sursa (job #2900109) | Cod sursa (job #2173701)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#define nmax 100005
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
vector<pair<int,int>>Q[nmax];
vector<int>stk,result;
int n,m,grad[nmax];
bool instk[nmax];
void read()
{
f>>n>>m;
for (int i=1; i<=m; ++i)
{
int e1,e2;
f>>e1>>e2;
Q[e1].push_back({e2,i});
Q[e2].push_back({e1,i});
++grad[e1];
++grad[e2];
}
}
void euler()
{
stk.push_back(1);
while (!stk.empty())
{
int nod=stk.back();
while (Q[nod].size()&&instk[Q[nod].back().second])
Q[nod].pop_back();
if (Q[nod].empty())
{
if (stk.size()>1)
g<<nod<<' ';
stk.pop_back();
}
else
{
int nextnod=Q[nod].back().first;
int nextindic=Q[nod].back().second;
instk[nextindic]=true;
stk.push_back(nextnod);
Q[nod].pop_back();
}
}
}
void solve()
{
for (int i=1; i<=n; ++i)
if (grad[i]&1)
{
g<<-1;
return;
}
euler();
reverse(stk.begin(),stk.end());
for (auto w:stk)
g<<w<<' ';
}
int main()
{
read();
solve();
return 0;
}