Cod sursa(job #3214112)

Utilizator tomaionutIDorando tomaionut Data 13 martie 2024 20:07:36
Problema Ciclu Eulerian Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
 
using namespace std;

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

int n, m, viz[100005], sol[100005], len;
vector <int> a[100005];
int st[100005], dr[100005], d[100005];

void Dfs(int x)
{
	while (a[x].size())
	{
		int i = a[x].back();
		a[x].pop_back();
		if (viz[i] == 0)
		{
			viz[i] = 1;
			Dfs(st[i] + dr[i] - x);
		}
	}
	sol[++len] = x;
}

int main()
{
	int i, x, y;
	fin >> n >> m;
	for (i = 1; i <= m; i++)
	{
		fin >> x >> y;
		d[x]++;
		d[y]++;
		st[i] = x;
		dr[i] = y;
		a[x].push_back(i);
		a[y].push_back(i);
	}

	for (i = 1; i <= n; i++)
		if (d[i] % 2 == 1)
		{
			fout << "-1\n";
			return 0;
		}

	Dfs(1);
	for (i = 1; i < len; i++)
		fout << sol[i] << " ";

	return 0;
}