Cod sursa(job #683226)

Utilizator Tucu94Andrei Tuculanu Tucu94 Data 20 februarie 2012 11:32:16
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include<fstream.h>
int k,i,j,x,ok,grad[1000],y,N,M,E[10000];
ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");

struct nod{
	int inf;
	nod* adr;
}*A[1000],*p,*q;

void citire(){
	f>>N>>M;
	for(int i=1;i<=M;i++)
	{
		f>>x>>y;
		grad[x]++;
		grad[y]++;
		p=new nod;
		p->inf=y;
		p->adr=A[x];
		A[x]=p;
		q=new nod;
		q->inf=x;
		q->adr=A[y];
		A[y]=q;
	}
}	
void afisare (){
	for(i=1;i<=N;i++)
	{p=A[i];
	while (p)
	{
		g<<p->inf<<" ";
		p=p->adr;
	}
	g<<"\n";
	}
	g<<"\n";
}
void ver()
{
	for(i=1;i<=N;i++)
		if (grad [i]%2!=0)
			{ok=1;
			g<<-1;
			break;
			}

}
int caut (int x)
{
	if (!A[x])
		return 0;
	return A[x]->inf;





}
void sterg(int x){
	int y=A[x]->inf;
	A[x]=A[x]->adr;
	//if(x!=y)
		if(A[y]->inf==x)
			A[y]=A[y]->adr;
		else
			for(p=A[y];p;p=p->adr)
				if(p->adr->inf==x)
				{
					p->adr=p->adr->adr;
					break;
				}

}
int main (){
	citire();

	ver();
	k=1;E[k]=1;
	while(k && !ok)
	{
		x=caut(E[k]);		
		if(x)
		{	
			E[++k]=x;
			sterg(E[k-1]);
		}
		else
		{
			if(k>1)
			g<<E[k]<<" ";
			k--;
		}
		
	}







return 0;
}