Cod sursa(job #1138276)

Utilizator harababurelPuscas Sergiu harababurel Data 9 martie 2014 20:35:24
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#define nmax 100005
#define mmax 500005
using namespace std;

vector <int> sol, v[nmax];
int n, m, a, b;
bool has = true;

void euler(int x) {
	int w;
	while(!v[x].empty()) {
		w = v[x][0];
		v[x][0] = v[x][v[x].size()-1];
		v[x].pop_back();

		for(int i=0; i<v[w].size(); i++)
			if(v[w][i] == x) {
				v[w][i] = v[w][v[w].size()-1];
				v[w].pop_back();
				break;
			}

		euler(w);
	}
	sol.push_back(x);
}
		

int main() {
	ifstream f("ciclueuler.in");
	ofstream g("ciclueuler.out");

	f>>n>>m;
	for(int i=1; i<=m; i++) {
		f>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i=1; i<=n; i++) if(v[i].size() % 2 == 1) has = false;

	if(has) {
		euler(1);
		for(int i=0; i<sol.size(); i++) g<<sol[i]<<" ";
		g<<"\n";
	}
	else g<<"-1\n";
	
	return 0;
}