Pagini recente » Cod sursa (job #754581) | Cod sursa (job #2333903) | Cod sursa (job #2232865) | Cod sursa (job #537364) | Cod sursa (job #1507022)
#include <stdio.h>
#define MAXN 100000
#define MAXM 500000
#define BUFF (1 << 20)
FILE *in;
int ult[MAXN], deg[MAXN], nod[2 * MAXM], next[2 * MAXM];
int st[MAXM + 1], d = 0;
int rez[MAXM + 1], drez;
char fol[MAXM];
char buff[BUFF];
int p = BUFF;
inline char cif(char ch){
if(ch >= '0' && ch <= '9')
return 1;
return 0;
}
inline char getch(){
if(p == BUFF){
fread(buff, 1, BUFF, in);
p = 0;
}
p++;
return buff[p - 1];
}
inline int getnum(){
char ch = getch();
while(!cif(ch))
ch = getch();
int rez = 0;
while(cif(ch)){
rez *= 10;
rez += ch - '0';
ch = getch();
}
return rez;
}
inline void ceuler(int x){
st[d] = x;
d++;
while(d > 0){
if(ult[st[d - 1]] == -1){
rez[drez] = st[d - 1];
drez++;
d--;
}
else if(fol[ult[st[d - 1]] / 2])
ult[st[d - 1]] = next[ult[st[d - 1]]];
else{
fol[ult[st[d - 1]] / 2] = 1;
st[d] = nod[ult[st[d - 1]]];
ult[st[d - 1]] = next[ult[st[d - 1]]];
d++;
}
}
}
int main(){
in = fopen("ciclueuler.in", "r");
int n, m, i, x, y, dr = 0;
n = getnum(); m = getnum();
for(i = 0; i < n; i++)
ult[i] = -1;
for(i = 0; i < m; i++){
x = getnum(); y = getnum();
x--; y--;
deg[x]++;
nod[dr] = x;
next[dr] = ult[y];
ult[y] = dr;
dr++;
deg[y]++;
nod[dr] = y;
next[dr] = ult[x];
ult[x] = dr;
dr++;
}
fclose(in);
FILE *out = fopen("ciclueuler.out", "w");
char g = 0;
for(i = 0; i < n; i++){
if(deg[i] & 1)
g = 1;
}
if(!g){
ceuler(0);
for(i = 0; i < drez - 1; i++)
fprintf(out, "%d ", rez[i] + 1);
}
else
fprintf(out, "-1");
return 0;
}