Pagini recente » Cod sursa (job #980653) | Cod sursa (job #1507384) | clasament-arhiva-educationala | Cod sursa (job #1843481) | Cod sursa (job #1482510)
#include <stdio.h>
#define MAXN 100000
#define MAXM 500000
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];
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(){
FILE *in = fopen("ciclueuler.in", "r");
int n, m, i, x, y, dr = 0;
fscanf(in, "%d%d", &n, &m);
for(i = 0; i < n; i++)
ult[i] = -1;
for(i = 0; i < m; i++){
fscanf(in, "%d%d", &x, &y);
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;
}