Pagini recente » Cod sursa (job #456007) | Cod sursa (job #368981) | Cod sursa (job #1186838) | Cod sursa (job #2741492) | Cod sursa (job #1921475)
#include <bits/stdc++.h>
#define nmax 500005
using namespace std;
vector <int> G[nmax];
bool viz[nmax];
int in[nmax],out[nmax];
int n,m,x,y;
int main()
{
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
fin>>n>>m;
for(int i=1;i<=m;i++){
fin>>x>>y;
G[x].push_back(i);
G[y].push_back(i);
in[i]=x;
out[i]=y;
}
for(int i=1;i<=n;i++){
if(G[i].size()&1){
fout<<"-1\n";
return 0;
}
}
vector <int> ans;
vector <int> st;
st.push_back(1);
while(!st.empty()){
int now=st.back();
if(!G[now].empty()){
int e=G[now].back();
G[now].pop_back();
if(!viz[e]){
viz[e]=true;
int next=::in[e]^::out[e]^now;
st.push_back(next);
}
}
else{
st.pop_back();
ans.push_back(now);
}
}
for (int i=0;i<ans.size()-1;++i) {
fout<<ans[i]<<' ';
}
fout <<'\n';
return 0;
}