Pagini recente » Cod sursa (job #2912793) | Cod sursa (job #1135805) | Cod sursa (job #1917725) | Cod sursa (job #1548170) | Cod sursa (job #2927495)
#pragma GCC optimize("O1")
#pragma GCC optimize("O2")
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
char buff[4096];
int pbuf=4095;
void readbuff(){
pbuf=0;fin.read(buff,4095);
}
int citire(){
int nr=0;
if(pbuf==4095){
readbuff();
}
while(buff[pbuf]<'0'||buff[pbuf]>'9'){
pbuf++;
if(pbuf==4095){
readbuff();
}
}
while(buff[pbuf]>='0'&&buff[pbuf]<='9'){
nr=nr*10+buff[pbuf]-'0';pbuf++;
if(pbuf==4095){
readbuff();
}
}
return nr;
}
vector<int>G[100005];
int grad[100005];
bool viz[100005];int dist=0;
void DFS(int nod){
viz[nod]=1;dist++;
for(int i=0;i<G[nod].size();i++){
if(viz[G[nod][i]]==0){
DFS(G[nod][i]);
}
}
}
int ciclu[1000005],stiva[1000005];
int oprit[100005];
int main()
{
int n,m,a,b;n=citire();m=citire();
for(int i=1;i<=m;i++){
a=citire();b=citire();
if(a==b){
G[a].push_back(b);
}
else{
G[a].push_back(b);
G[b].push_back(a);
grad[a]++;grad[b]++;
}
}
int ok=0;
for(int i=1;i<=n;i++){
if(grad[i]%2==1){ok=1;break;}
}
dist=0;DFS(1);
if(dist!=n){ok=1;}
if(ok==1){
fout<<"-1"<<'\n';return 0;
}
int pozciclu=0,pozst;
pozst=1;stiva[pozst]=1;
vector<int>::iterator it;
while(1){
if(pozst==0){break;}
if(G[stiva[pozst]].size()==0){
pozciclu++;ciclu[pozciclu]=stiva[pozst];
pozst--;
}
else{
pozst++;stiva[pozst]=G[stiva[pozst-1]][0];
swap(G[stiva[pozst-1]][0],G[stiva[pozst-1]][G[stiva[pozst-1]].size()-1]);
G[stiva[pozst-1]].erase(G[stiva[pozst-1]].end()-1,G[stiva[pozst-1]].end());
for(int i=0;i<G[stiva[pozst]].size();i++){
if(G[stiva[pozst]][i]==stiva[pozst-1]){
swap(G[stiva[pozst]][i],G[stiva[pozst]][G[stiva[pozst]].size()-1]);
G[stiva[pozst]].erase(G[stiva[pozst]].end()-1,G[stiva[pozst]].end());
break;
}
}
}
}
for(int i=pozciclu;i>1;i--){
fout<<ciclu[i]<<" ";
}fout<<'\n';
return 0;
}