Cod sursa(job #2645094)

Utilizator sebimihMihalache Sebastian sebimih Data 27 august 2020 00:05:00
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

const int N = 1e5 + 5, M = 5e5 + 5;

// catre nod si ID
vector<pair<int, int>> g[N];
vector<int> ordine;
bool vizitat[M];

void dfs(int start)
{
	stack<int> stiva;
	stiva.push(start);

	while (!stiva.empty())
	{
		int nod = stiva.top();
		stiva.pop();

		if (g[nod].size() == 0)
		{
			ordine.push_back(nod);
		}
		else
		{
			int NextNod = g[nod].back().first;
			int ID = g[nod].back().second;

			g[nod].pop_back();
			stiva.push(nod);

			if (!vizitat[ID])
			{
				vizitat[ID] = true;
				stiva.push(NextNod);
			}
		}
	}
}

int main()
{
	int n, m;
	fin >> n >> m;

	for (int i = 0; i < m; i++)
	{
		int x, y;
		fin >> x >> y;
		g[x].push_back({ y, i });
		g[y].push_back({ x, i });
	}

	for (int i = 1; i <= n; i++)
	{
		if (g[i].size() % 2 == 1)
		{
			fout << -1;
			return 0;
		}
	}

	dfs(1);

	ordine.pop_back();
	for (int x : ordine)
	{
		fout << x << ' ';
	}

	return 0;
}