Cod sursa(job #1870650)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 6 februarie 2017 20:10:58
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <vector>
#define SZ(x) ((int) (x).size())
using namespace std;

const int NMAX = 100000;

vector<int> G[NMAX];
int E[NMAX];

int main(void) {
	ifstream cin("ciclueuler.in");
	ofstream cout("ciclueuler.out");
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int n, m; cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y; x--; y--;
		E[i] = x ^ y;
		G[x].push_back(i);
		G[y].push_back(i);
	}
	
	for (int i = 0; i < n; i++) {
		if (SZ(G[i]) & 1) {
			cout << "-1\n";
		}
	}

	vector<int> stk;
	stk.push_back(0);
	while (not stk.empty()) {
		const int node = stk.back();
		while (not G[node].empty() and E[G[node].back()] == -1) {
			G[node].pop_back();
		}	
		if (G[node].empty()) {
			stk.pop_back();
			if (not stk.empty()) {
				cout << node + 1 << ' ';
			}
		} else {
			stk.push_back(E[G[node].back()] ^ node);
			E[G[node].back()] = -1;
			G[node].pop_back();
		}
	}
}