Cod sursa(job #728544)

Utilizator gabriel93Robu Gabriel gabriel93 Data 28 martie 2012 19:39:52
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<stdio.h>
#include<vector>
#define Nmax 1001000
using namespace std;
int n,m;

vector< pair <int,int> > g[Nmax];
int nr[Nmax];
char viz[Nmax];
int st[Nmax];

void citire()
{
	int i,x,y;
	scanf("%d %d",&n,&m);
	for(i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		g[x].push_back(make_pair(y,i));
		g[y].push_back(make_pair(x,i));
		++nr[x];
		++nr[y];
	}
}

void dfs()
{
	vector<pair <int,int> >::iterator it;
	int p,x,ok;
	st[1]=1;
	p=1;
	while(p)
	{
		x=st[p];
		ok=1;
		for(it=g[x].begin();it!=g[x].end();++it)
			if(viz[(*it).second]==0)
			{
				viz[(*it).second]=1;
				ok=0;
				++p;
				st[p]=(*it).first;
				break;
			}
		if(ok)
		{
			if(p>1)
				printf("%d ",x);
			--p;
		}
	}
}

int main()
{
	int i,ok;
	freopen("ciclueuler.in","r",stdin);
	freopen("ciclueuler.out","w",stdout);
	citire();
	ok=1;
	for(i=1;i<=n;++i)
		if(nr[i]&1)
		{
			ok=0;
			printf("-1");
			break;
		}
	if(ok)
		dfs();
	fclose(stdin);
	fclose(stdout);
	return 0;
}