Cod sursa(job #582196)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 15 aprilie 2011 00:14:45
Problema Ciclu Eulerian Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define nmax 100010
#define mmax 500010
#define f first
#define s second

vector <pair<int, int> > g[nmax];
stack <int> s, mc, t;
int n, m, a[mmax], l;
char u[mmax];

int df_verif()
{
	int nod, i, v;
	s.push(1);
	mc.push(0);
	while (!s.empty())
	{
		if (!u[mc.top()])
		{
			nod=s.top();
			u[mc.top()]=1;
			s.pop();
			mc.pop();
			for (i=0; i<g[nod].size(); i++)
				if (!u[g[nod][i].s])
				{
					v=g[nod][i].f;
					s.push(v);
					mc.push(g[nod][i].s);
				}
		} else 
		{
			s.pop();
			mc.pop();
		}
	}
	for (i=1; i<=m; i++)
		if (!u[i]) return 0;
	return 1;
}

void df()
{
	int nod, v, i, prec=0;
	s.push(1);
	mc.push(0);
	t.push(0);
	while (!s.empty())
	{
		if (!u[mc.top()])
		{
			nod=s.top();
			u[mc.top()]=1;
			while (t.top()!=prec && l)
			{
				printf("%d ",a[l]);
				l--;
			}
			a[++l]=nod;
			prec=nod;
			s.pop();
			t.pop();
			mc.pop();
			for (i=0; i<g[nod].size(); i++)
				if (!u[g[nod][i].s])
				{
					v=g[nod][i].f;
					s.push(v);
					t.push(nod);
					mc.push(g[nod][i].s);
				}
		} else 
		{
			s.pop();
			t.pop();
			mc.pop();
		}
	}
}

int main()
{
	freopen("ciclueuler.in","r",stdin);
	freopen("ciclueuler.out","w",stdout);
	scanf("%d %d", &n, &m);
	int i, x, y;
	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));
	}
	/*if (!df_verif())
	{
		printf("-1\n");
		return 0;
	}*/
	for (i=0; i<=m; i++) u[i]=0;
	l=0;
	df();
	while (l>1)
	{
		printf("%d ",a[l]);
		l--;
	}
	printf("\n");
	return 0;
}