Pagini recente » Cod sursa (job #1367155) | Cod sursa (job #607080) | Cod sursa (job #543660) | Cod sursa (job #3037900) | Cod sursa (job #2443014)
#include<fstream>
#include<vector>
using namespace std;
#define maxn 100005
#define maxm 500005
int n,m,g[maxn],stiva[maxm],rez[maxm] , prim=1, ultim=1,k=1;
vector <int>v[maxn];
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
void read(){
int x,y;
for(int i=1; i<=m; i++){
cin>>x>>y;
if(x==y){
v[x].push_back(y);
g[x]++;
} else{
v[x].push_back(y);
v[y].push_back(x);
g[x]++; g[y]++;
}
}
}
void euler(){
int x=prim;
while(prim<=ultim){
while(g[stiva[x]]){
stiva[++ultim]=v[stiva[x]][g[stiva[x]]-1];
//cout<<v[stiva[x]][g[stiva[x]]-1]<<' ';
v[stiva[x]].pop_back();
g[stiva[x]]--;
if(stiva[x]!=stiva[ultim]){
for(int i=0; i<g[stiva[ultim]]; i++){
if(v[stiva[ultim]][i]==stiva[x]){
v[stiva[ultim]].erase (v[stiva[ultim]].begin()+i);
g[stiva[ultim]]--;
break;
}
}
}
prim++;
euler();
}
prim++;
rez[k++]=stiva[x];
}
}
int main(){
cin>>n>>m;
read();
stiva[1]=1;
euler();
k-=2;
if(k!=m)
cout<<-1;
else
for(int i=1; i<=k; i++)
cout<<rez[i]<<' ';
return 0;
}
/*
4 6
1 2
1 3
2 2
2 3
3 4
3 4
*/