Cod sursa(job #275004)

Utilizator Andrei_ScorpioAndreiana Andrei Daniel Andrei_Scorpio Data 10 martie 2009 10:06:34
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include<stdio.h>
#define Nmax 101000
#define Mmax 501000
long varf,gr[Nmax],vf,st[Mmax],n,m,sol[Mmax];
struct elem
{
 long inf;
 elem *urm;
}*a[Nmax];

void citire()
{
 elem *p;
 long x,y;
 freopen("ciclueuler.in","r",stdin);
 scanf("%ld%ld",&n,&m);
 for(long i=0;i<m;i++)
 {      scanf("%ld%ld",&x,&y);
	p=new elem;
	p->inf=x;
	p->urm=a[y];
	a[y]=p;
	p=new elem;
	p->inf=y;
	p->urm=a[x];
	a[x]=p;
	gr[x]++;
	gr[y]++;
 }
 fclose(stdin);
}

int verif()
{
 for(long i=1;i<=n;i++)
	if(gr[i]%2==1 || gr[i]==0)
		return 0;
 return 1;

}

void sterg(long x,long y)
{
 elem *p,*q;
 p=a[y];
 if(a[y]->inf!=x)
 {	while(a[y]->urm->inf!=x)
		a[y]=a[y]->urm;
	q=a[y]->urm;
	a[y]->urm=a[y]->urm->urm;
	a[y]=p;
	delete q;
 }
 else
	{
	a[y]=a[y]->urm;
	delete p;
	}
}

void parcurg()
{
 st[vf]=1;
 while(vf>=0)
 if(gr[st[vf]]<=0)
 {	if(vf!=0)
		sol[++varf]=st[vf];
		//printf("%ld ",st[vf]);
	vf--;
 }
 else
 {	st[vf+1]=a[st[vf]]->inf;
	long qw=a[st[vf]]->inf;
	a[st[vf]]=a[st[vf]]->urm;
	sterg(st[vf],qw);
	gr[st[vf]]--;
	vf++;
	gr[st[vf]]--;
 }
}

int main()
{
 citire();
 freopen("ciclueuler.out","w",stdout);
 if(verif())
	{
	parcurg();
	if(varf==m)
		for(long i=1;i<=varf;i++)
			printf("%ld ",sol[i]);
	else printf("-1\n");
	}
 else
	printf("-1\n");
 fclose(stdout);
 return 0;
}