Pagini recente » Cod sursa (job #3182986) | Cod sursa (job #407511) | Cod sursa (job #923164) | Cod sursa (job #2323122) | Cod sursa (job #1913508)
#include <fstream>
#include <vector>
#define NMAX 100010
#define MMAX 500010
using namespace std;
vector <int> G[NMAX];
bool fol[MMAX];
int n,m,i,from[NMAX],to[NMAX];
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int main()
{
f>>n>>m;
for(i=1;i<=m;++i){
int x,y;
f>>x>>y;
G[x].push_back(i);
G[y].push_back(i);
from[i]=x;
to[i]=y;
}
bool ok=true;
for(i=1;i<=n&&ok==true;++i)
if(G[i].size()&1)
ok=false;
if(ok==false)
g<<"-1\n";
else{
vector <int> st;
vector <int> sol;
st.push_back(1);
while(!st.empty()){
int nod=st.back();
if(!G[nod].empty()){
int e=G[nod].back();
G[nod].pop_back();
if(!fol[e]){
fol[e]=true;
int x=from[e];
if(x==nod)
x=to[e];
st.push_back(x);
}
}else{
st.pop_back();
sol.push_back(nod);
}
}
sol.pop_back();
for(vector <int>::iterator it=sol.begin();it<sol.end();++it)
g<<*it<<" ";
}
return 0;
}