Pagini recente » Cod sursa (job #3194075) | Cod sursa (job #950626) | Cod sursa (job #725594) | Cod sursa (job #1012978) | Cod sursa (job #1482517)
#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], prev[2 * MAXM];
int st[MAXM + 1], d = 0;
int ac[MAXM + 1], k[MAXM + 1], da = 0;
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;
}
void ceuler(int x){
ac[da] = x;
k[da] = ult[x];
da++;
int poz;
while(da > 0){
if(k[da - 1] == -1){
st[d] = ac[da - 1] + 1;
d++;
da--;
}
else{
x = ac[da - 1];
poz = k[da - 1];
k[da - 1] = next[k[da - 1]];
if(fol[poz / 2]){
if(prev[poz] == -1)
ult[x] = next[poz];
else
next[prev[poz]] = next[poz];
}
else{
fol[poz / 2] = 1;
ac[da] = nod[poz];
k[da] = ult[nod[poz]];
da++;
}
}
}
}
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]++;
deg[y]++;
nod[dr] = x;
next[dr] = ult[y];
prev[ult[y]] = dr;
ult[y] = dr;
prev[dr] = -1;
dr++;
nod[dr] = y;
next[dr] = ult[x];
prev[ult[x]] = dr;
ult[x] = dr;
prev[dr] = -1;
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 < d - 1; i++)
fprintf(out, "%d ", st[i]);
}
else
fprintf(out, "-1");
return 0;
}