Cod sursa(job #2722332)

Utilizator xCata02Catalin Brita xCata02 Data 12 martie 2021 19:13:26
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda no-time-rest-v3 Marime 1.33 kb
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ld long double
#define endl "\n"
 
const int Nmax = 100010;
const int MODULO = 1e9 + 7;
const int oo = -1e9;

vector < pair < int , int > > v;
int n, m;
int frecv[Nmax];
bool viz[5 * Nmax];

vector < int > g[Nmax];
stack < int > stk;

void solve() {
	cin >> n >> m;
	v.push_back({0,0});
	for(int i = 1; i <= m; i++) {
		int x, y; cin >> x >> y;
		v.push_back({x, y});
		g[x].push_back(i);
		g[y].push_back(i);
		frecv[x]++; frecv[y]++;
	}

	for(int i = 1; i <= n; i++) {
		if(frecv[i] % 2 == 1) {
			cout << -1;
			return;
		}
	}

	stk.push(1);

	while(!stk.empty()) {
		int sus = stk.top();
		if(!g[sus].empty()) {
			int jos = g[sus].back();
			g[sus].pop_back();
			if(viz[jos] == false) {
				int nod;
				if(sus == v[jos].first) {
					nod = v[jos].second;
				} else {
					nod = v[jos].first;
				}
				stk.push(nod);
				viz[jos] = 1;
			}
		} else {
			if(stk.size() > 1) {
				cout << sus << " ";
			}
			stk.pop();
		}
	}
}
	
void fisier() {
	freopen("ciclueuler.in" , "r", stdin);
	freopen("ciclueuler.out", "w", stdout);
}

 
int main() {
	ios_base::sync_with_stdio(0);
 
	cin .tie(0);
	cout.tie(0);

	fisier();
 
	int testCases = 1;
	//cin >> testCases;
	while(testCases--) {
		solve();
	}
}