Pagini recente » Cod sursa (job #969372) | Cod sursa (job #492867) | Cod sursa (job #1298894) | Cod sursa (job #2211944) | Cod sursa (job #1877210)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
#define MAXN 100010
vector <int> G[MAXN],ans;
stack <int> st;
int viz[MAXN],n,m,gr[MAXN],x,y,w,urm;
int main()
{
f>>n>>m;
while(m--)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=1; i<=n; ++i)
{
if(G[i].size()%2)
{
g<<-1;
return 0;
}
}
st.push(1);
while(st.size())
{
w=st.top();
if(!G[w].empty())
{
urm=G[w][G[w].size()-1];
G[w].pop_back();
st.push(urm);
for(auto &it:G[urm])
if(it==w)
{
swap(it,G[urm][G[urm].size()-1]);
G[urm].pop_back();
break;
}
}
else
{
ans.push_back(w);
st.pop();
}
}
ans.pop_back();
for(auto it:ans)
g<<it<<' ';
return 0;
}