Cod sursa(job #1138730)

Utilizator harababurelPuscas Sergiu harababurel Data 10 martie 2014 15:23:09
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 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() {
	st.push(1);
	while(!st.empty()) {
		int x = st.top();
		
		if(!v[x].empty()) {
			int y = v[x][v[x].size()-1];
			v[x].pop_back();
			for(int i=0; i<v[y].size(); i++) if(v[y][i] == x) { v[y][i] = v[y][v[y].size()-1]; v[y].pop_back(); break; }

			st.push(y);
			continue;
		}

		sol.push_back(x);
		st.pop();
	}
}


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();
		for(int i=0; i<sol.size(); i++) g<<sol[i]<<" ";
		g<<"\n";
	}
	else g<<"-1\n";
	
	return 0;
}