Cod sursa(job #703760)

Utilizator mottyMatei-Dan Epure motty Data 2 martie 2012 14:16:20
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#include <vector>

using namespace std;

const int N = 100001;
const int M = 500005;

int n, m;
unsigned int cur[N];
bool used[M];

vector < pair<int,int> > e;
vector <int> sol;
vector <int> g[N];

void ReadGraph()
{
	scanf("%d%d", &n, &m);

	for (int i = 0; i < m; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);

		e.push_back(make_pair(a, b));
		g[a].push_back(i);
		g[b].push_back(i);
	}
}

inline int otherNode(int node, int edge)
{
	if (e[edge].first == node)
		return e[edge].second;

	return e[edge].first;
}

void DiggIn(int node)
{
	bool alive = false;

	while (cur[node] < g[node].size()) {
		int edge = g[node][cur[node]];

		if (!used[edge])
		{
			int next = otherNode(node, edge);

			used[edge] = true;
			alive = true;
			DiggIn(next);
		}

		++cur[node];
	}

	if(!alive)
		sol.push_back(node);
	else
		DiggIn(node);
}

void OutputSolution()
{
	int position = sol.size();

	while (position > 1) {
		--position;
		printf("%d ", sol[position]);
	}

	printf("\n");
}

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

	ReadGraph();
	DiggIn(1);
	OutputSolution();

	return 0;
}