Cod sursa(job #1438920)

Utilizator VladutZ94FMI Chichirau Vlad Vasile VladutZ94 Data 21 mai 2015 04:55:01
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <list>
#include <deque>
#define NMAX 100001

int n, m;
std::list<int>graf[NMAX];
std::deque<int> coada;
std::list<int>::iterator it;

void citire()
{
	int x, y;
	std::ifstream f("ciclueuler.in");
	f >> n >> m;
	for (int i = 1; i <= m;i++)
	{
		f >> x >> y;
		graf[x].push_back(y);
		graf[y].push_back(x);
	}
}

int main()
{
	citire();
	std::ofstream g("ciclueuler.out");
	bool ok = false;
	int x, y;
	for (int i = 1; i <= n; i++)
	{
		if (graf[i].size() % 2 == 0)
		{
			ok = true;
			break;
		}
	}
	if (!ok)
	{
		g << "-1\n";
		return 0;
	}
	coada.push_front(1);
	while (!coada.empty())
	{
		x = coada.front();
		if (graf[x].size() == 0)
		{
			g << x << " ";
			coada.pop_front();
		}
		else
		{
			y = graf[x].front();
			graf[x].pop_front();
			coada.push_front(y);

			for (it = graf[y].begin(); it != graf[y].end(); it++)
			{
				if (*it == x)
				{
					graf[y].erase(it);
					break;
				}
			}
		}
	}
	return 0;
}