Pagini recente » Cod sursa (job #708558) | Cod sursa (job #115124) | Cod sursa (job #678706) | Cod sursa (job #140084) | Cod sursa (job #587307)
Cod sursa(job #587307)
#include <stdio.h>
#include <vector>
#define max_n 100001
#define max_m 500001
using namespace std;
vector <int> a[max_n];
int x[max_m],y[max_m],g[max_n],nr[max_m],c[max_m],s[max_m];
bool ap[max_m];
int i,n,m,j,vf,z,u;
int main () {
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1; i<=m; i++) {
scanf("%d%d",&x[i],&y[i]);
a[x[i]].push_back(i);
a[y[i]].push_back(i);
g[x[i]]++;
g[y[i]]++;
}
vf=0; u=0;
bool ok=true;
for (i=1; i<=n; i++)
if (g[i]%2==1) {
ok=false;
printf("-1\n");
}
if (ok) {
vf=1;
s[vf]=1;
while (vf) {
while (nr[s[vf]]<g[s[vf]] && vf+u!=m+1) {
z=a[s[vf]][nr[s[vf]]];
nr[s[vf]]++;
if (!ap[z]) {
ap[z]=true;
if (s[vf]==x[z]) s[++vf]=y[z];
else s[++vf]=x[z];
}
}
c[++u]=s[vf--];
}
u--;
if (u<m) printf("-1\n");
else {
for (i=1; i<u; i++) printf("%d ",c[i]);
printf("%d\n",c[u]);
}
}
return 0;
}