Cod sursa(job #238196)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 31 decembrie 2008 21:26:17
Problema Ciclu Eulerian Scor Ascuns
Compilator cpp Status done
Runda Marime 1 kb
#include <stdio.h>
#include <assert.h>
#include <vector>

using namespace std;

#define maxn 100010
#define maxx 500010

int N, M, L;
int X[maxx], Y[maxx];
vector <int> A[maxn];
int G[maxn];
char U[maxx];
int S[maxx];

inline void DFS(int nod)
{
	int i;

	for (i = 0; i < G[nod]; i++)
		if (!U[A[nod][i]])
		{
			U[A[nod][i]] = 1;

			if (X[A[nod][i]] == nod) DFS(Y[A[nod][i]]);
			else DFS(X[A[nod][i]]);
		}

	S[++L] = nod;
}

int main()
{
	freopen("ciclueuler.in", "r", stdin);
	freopen("ciclueuler.out", "w", stdout);

	int i;

	scanf("%d %d ", &N, &M);

	assert(1 <= N && N <= 100000);
	assert(1 <= M && M <= 500000);

	for (i = 1; i <= M; i++) 
	{
		scanf("%d %d ", &X[i], &Y[i]);

		assert(1 <= X[i] && X[i] <= N);
		assert(1 <= Y[i] && Y[i] <= N);

		A[X[i]].push_back(i);
		A[Y[i]].push_back(i);
	}

	for (i = 1; i <= N; i++) G[i] = A[i].size();

	DFS(1);

	for (i = 1; i <= M; i++)
		if (!U[i])
		{
			printf("-1\n");
			return 0;
		}

	for (i = 1; i < L; i++) printf("%d ", S[i]);
	printf("\n");

	return 0;
}