Cod sursa(job #2681300)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 5 decembrie 2020 11:26:38
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <list>
#include <stack>

using namespace std;

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

struct edg{
	int a, b;
	bool ok;
	int ott(int x){
		if(x == a)return b;
		else return a;
	}
};

int n, m;
list<int> gra[100041];
edg edgy[500041];
int grad[100041];

int nxt = 0;
stack<int> sta;

void stakoneta(){
	sta.push(1);
	
	while(!sta.empty()){
		int a = sta.top();
		auto &gr = gra[a];
		
		while(!gr.empty() && !edgy[*gr.begin()].ok){
			gr.erase(gr.begin());
		}
		if(!gr.empty()){
			int ei = *gr.begin();
			gr.erase(gr.begin());
			
			edg &e = edgy[ei];
			e.ok = false;
			
			int b = e.ott(a);
			sta.push(b);
		}else{
			sta.pop();
			if(nxt != 0)fout << nxt << " ";
			nxt = a;
		}
		
	}
}

int main(){
	// ios_base::sync_with_stdio(false);
	fin >> n >> m;
	for(int i = 0; i < m; ++i){
		int a, b;fin>> a >> b;
		edgy[i] = {a,b,true};
		grad[a]++;gra[a].push_back(i);
		grad[b]++;gra[b].push_back(i);
	}
	
	bool ok = true;;
	for(int i = 1; i <= n; ++i){
		if(grad[i]%2 != 0){
			ok = false;
			break;
		}
	}
	
	if(ok)stakoneta();
	else fout << -1;
	return 0;
}