Cod sursa(job #1138277)

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

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

void euler(int x) {
	int w;
	st.push(1);

	while(!st.empty()) {
		int x = st.top();
		st.pop();

		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;
				}
		st.push(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;
}