Cod sursa(job #587307)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 4 mai 2011 17:00:48
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#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;
}