Pagini recente » Cod sursa (job #1205639) | Cod sursa (job #421318) | Cod sursa (job #542023) | Cod sursa (job #2021455) | Cod sursa (job #2575784)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fi("ciclueuler.in");
ofstream fo("ciclueuler.out");
const int nmax=1e5;
int n, m;
int used[5*nmax+5];
vector <int> sol;
vector <pair <int, int> > G[nmax+5];
stack <int> S;
int main()
{
fi>>n>>m;
for(int i=1; i<=m; i++)
{
int x, y;
fi>>x>>y;
G[x].push_back({y, i});
G[y].push_back({x, i});
}
for(int i=1; i<=n; i++)
if(G[i].size()&1)
{
fo<<-1;
return 0;
}
S.push(1);
while(!S.empty())
{
int nod=S.top();
while(!G[nod].empty())
if(used[G[nod].back().second])
G[nod].pop_back();
else
break;
if(!G[nod].empty())
{
pair <int, int> vec=G[nod].back();
G[nod].pop_back();
used[vec.second]=1;
S.push(vec.first);
}
else
{
sol.push_back(nod);
S.pop();
}
}
sol.pop_back();
for(auto i:sol)
fo<<i<<" ";
fi.close();
fo.close();
return 0;
}