Pagini recente » Cod sursa (job #1212940) | Cod sursa (job #809076) | Cod sursa (job #2812740) | Cod sursa (job #2278843) | Cod sursa (job #1702225)
#include <cstdio>
#include <cctype>
#include <list>
#include <queue>
#define LIM 2000000
#define MAXN 100010
#define MAXM 500010
using namespace std;
FILE *fin;
int p=LIM-1;
char str[LIM];
inline void avans(){
if(++p==LIM){
fread(str, 1, LIM, fin);
p=0;
}
}
inline int getnr(){
int nr=0;
while(isdigit(str[p])==0)
avans();
while(isdigit(str[p])){
nr=nr*10+str[p]-'0';
avans();
}
return nr;
}
int sol[MAXM+1], s;
int vf[2*MAXM], M, lst[MAXN+1], next[2*MAXM], fol[MAXM];
int viz[MAXN+1], grad[MAXN+1], stiv[MAXM+1];
inline void add(int x, int y){
vf[++M]=y;
next[M]=lst[x];
lst[x]=M;
}
inline void bfs(int nod){
int q[MAXN], b, e, p;
b=0; e=1; q[b]=nod;
while(b<e){
nod=q[b++];
viz[nod]=1;
p=lst[nod];
while(p){
if(viz[vf[p]]==0)
q[e++]=vf[p];
p=next[p];
}
}
}
void euler(int nod){
int st=0, mu;
stiv[st]=nod;
while(st>=0)
if(lst[stiv[st]]==0)
sol[s++]=stiv[st--];
else if(fol[(lst[stiv[st]]-1)/2])
lst[stiv[st]]=next[lst[stiv[st]]];
else{
mu=lst[stiv[st]];
lst[stiv[st]]=next[lst[stiv[st]]];
fol[(mu-1)/2]=1;
stiv[++st]=vf[mu];
}
}
int main()
{
FILE *fout;
int n, m, i, x, y;
fin=fopen("ciclueuler.in", "r");
fscanf(fin, "%d%d", &n, &m);
for(i=0; i<m; i++){
x=getnr(); y=getnr();
grad[x]++; grad[y]++;
add(x, y);
add(y, x);
}
fclose(fin);
bfs(1);
for(i=1, x=0; i<=n && x==0; i++)
if((grad[i]&1) || viz[i]==0)
x=1;
fout=fopen("ciclueuler.out", "w");
if(x)
fprintf(fout, "-1\n");
else{
euler(1);
for(i=0; i<s-1; i++)
fprintf(fout, "%d ", sol[i]);
fprintf(fout, "\n");
}
return 0;
}