Pagini recente » Cod sursa (job #416093) | Cod sursa (job #183488) | Cod sursa (job #275204) | Cod sursa (job #3226630) | Cod sursa (job #2927555)
#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;
}
struct muchie{
int poz,nod;
};
vector<muchie>G[100005];
int grad[100005];
bool viz[100005];int dist=0;
void DFS(int nod1){
viz[nod1]=1;dist++;
for(int i=0;i<G[nod1].size();i++){
if(viz[G[nod1][i].nod]==0){
DFS(G[nod1][i].nod);
}
}
}
int ciclu[1000005],stiva[1000005];
int oprit[100005];
bool f[500005];
muchie val;
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){
val.nod=b;val.poz=i;
G[a].push_back(val);
}
else{
val.poz=i;val.nod=b;
G[a].push_back(val);
val.nod=a;
G[b].push_back(val);
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;
while(pozst>0){
while(G[stiva[pozst]].size()>0&&f[G[stiva[pozst]][G[stiva[pozst]].size()-1].poz]==1){
G[stiva[pozst]].erase(G[stiva[pozst]].end()-1,G[stiva[pozst]].end());
}
if(G[stiva[pozst]].size()==0){
pozciclu++;ciclu[pozciclu]=stiva[pozst];
pozst--;
}
else{
pozst++;stiva[pozst]=G[stiva[pozst-1]][G[stiva[pozst-1]].size()-1].nod;
f[G[stiva[pozst-1]][G[stiva[pozst-1]].size()-1].poz]=1;
G[stiva[pozst-1]].erase(G[stiva[pozst-1]].end()-1,G[stiva[pozst-1]].end());
}
}
for(int i=pozciclu;i>1;i--){
fout<<ciclu[i]<<" ";
}fout<<'\n';
return 0;
}