Pagini recente » Cod sursa (job #2968446) | Cod sursa (job #389007) | Cod sursa (job #2153123) | Cod sursa (job #1622866) | Cod sursa (job #1602016)
#include<cstdio>
using namespace std;
const int nMax = 100000, mMax = 500000;
int muchie[mMax * 2 + 1], nodV[mMax * 2 + 1], urm[mMax * 2 + 1], lst[nMax + 1], sol[mMax + 1];
bool viz[nMax], ut[mMax + 1], grad[nMax + 1];
int nr = 0, nrS = 0;
FILE *in, *out;
inline void adauga(int i, int x, int y){
nodV[++nr] = y;
urm[nr] = lst[x];
lst[x] = nr;
muchie[nr] = i;
}
void dfs(){
int nod = 1, pos, vec;
while(nod){
pos = lst[nod];
sol[++nrS] = nod;
viz[nod] = true;
vec = 0;
while(pos && !vec){
if(!ut[muchie[pos]]){
ut[muchie[pos]] = true;
vec = nodV[pos];
}
pos = urm[pos];
}
nod = vec;
}
}
int main (){
in = fopen("ciclueuler.in","r");
out = fopen("ciclueuler.out","w");
int n, m; fscanf(in,"%d%d", &n, &m);
for(int i = 1, x, y; i <= m ; ++i){
fscanf(in,"%d%d", &x, &y);
adauga(i, x, y);
adauga(i, y, x);
grad[x] = 1 - grad[x];
grad[y] = 1 - grad[y];
}
dfs();
for(int i = 1 ; i <= n ; ++i){
if(!viz[i] || grad[i]){
fprintf(out,"-1\n");
return 0;
}
}
for(int i = 1 ; i < nrS ; ++i)
fprintf(out,"%d ", sol[i]);
fprintf(out,"\n");
return 0;
}