Pagini recente » Cod sursa (job #2329426) | Cod sursa (job #1641376) | Cod sursa (job #974638) | Cod sursa (job #225984) | Cod sursa (job #1880194)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
#define MAXN 100010
vector <pair<int,int> > G[MAXN];
vector <int> ans;
stack <int> st;
int n,m,x,y,w,urm,nr,poz,poz1,poz2;
int main()
{
f>>n>>m;
while(m--)
{
f>>x>>y;
poz1=G[y].size();
poz2=G[x].size();
G[x].push_back(make_pair(y,poz1));
G[y].push_back(make_pair(x,poz2));
}
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].back().first;
poz=G[w].back().second;
st.push(urm);
if(poz<G[urm].size())
G[urm][poz]=G[urm].back();
else
for(auto &it:G[urm])
if(it.first==w)
{
it=G[urm].back();
break;
}
G[urm].pop_back();
G[w].pop_back();
}
else
{
ans.push_back(w);
st.pop();
}
}
ans.pop_back();
for(auto it:ans)
g<<it<<' ';
return 0;
}