Pagini recente » Borderou de evaluare (job #2006605) | Borderou de evaluare (job #1268675) | Borderou de evaluare (job #2236554) | Borderou de evaluare (job #2074736) | Cod sursa (job #1482501)
#include <stdio.h>
#define MAXN 100000
#define MAXM 500000
int ult[MAXN], deg[MAXN], nod[2 * MAXM], next[2 * MAXM];
int st[MAXM + 1], d = 0;
char fol[MAXM];
void ceuler(int x){
int poz = ult[x], prev = -1;
while(poz != -1){
if(fol[poz / 2]){
if(prev == -1)
ult[x] = next[poz];
else
next[prev] = next[poz];
}
else{
fol[poz / 2] = 1;
ceuler(nod[poz]);
}
prev = poz;
poz = next[poz];
}
st[d] = x + 1;
d++;
}
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];
ult[y] = dr;
dr++;
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 < d - 1; i++)
fprintf(out, "%d ", st[i]);
}
else
fprintf(out, "-1");
return 0;
}