Cod sursa(job #1474208)

Utilizator greenadexIulia Harasim greenadex Data 21 august 2015 14:42:23
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define mp make_pair

using namespace std;

const string name = "ciclueuler",
             in_file = name + ".in",
             out_file = name + ".out";

ifstream fin(in_file);
ofstream fout(out_file);

const int MMAX = 5e5 + 5, NMAX = 1e5 + 1;

int n, m, vizm[MMAX], vizn[NMAX], grad[NMAX], ciclu[MMAX], cnt;
vector<pii> gr[MMAX];
vector<int> v;

void dfs(int node) {
	vizn[node] = 1;

	for (auto it : gr[node])
		if (!vizn[it.f])
			dfs(it.f);
}

int main() {
	fin >> n >> m;
	for (int a, b, i = 1; i <= m; i++) {
		fin >> a >> b;
		gr[a].pb(mp(b, i));
		gr[b].pb(mp(a, i));
		grad[a]++, grad[b]++;
	}

	for (int i = 1; i <= n; i++)
		if ((grad[i] & 1)) {
			fout << -1;
			return 0;
		}

	dfs(1);

	for (int i = 1; i <= n; i++)
		if (!vizn[i]) {
			fout << -1;
			return 0;
		}

	v.pb(1);
	while (!v.empty()) {
		int node = v.back(), next, cnt;
		if (!gr[node].empty()) {
			next = gr[node].back().f;
			cnt = gr[node].back().s;

			if (!vizm[cnt]) {
				vizm[cnt] = 1;
				v.pb(next);
			}

			gr[node].pop_back();
		} else {
			fout << v.back() << ' ';
			v.pop_back();
		}
	}
	return 0;
}