Cod sursa(job #2580048)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 13 martie 2020 11:42:36
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.11 kb
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
#define Nmax 500005
const long double PI = acos(-1);

int muchii[2 * Nmax], nr;
bool viz[2 * Nmax];
vector<int> G[100005];
stack<int> st, st2;
void DFS(int nod){
	st2.push(nod);
	while(!st2.empty()){
		int nd = st2.top();
		if(!G[nd].empty()){
			auto it = G[nd].back();
			G[nd].pop_back();
			if(viz[it] == 0)
			{
				viz[it] = viz[it ^ 1] = 1;
				st2.push(muchii[it]);
			}
		}
		else {
			st.push(nd);
			st2.pop();
		}
	}
}

int main(){
  ios::sync_with_stdio(false);
  freopen("ciclueuler.in", "r", stdin);
  freopen("ciclueuler.out", "w", stdout);
  int n, m;
  cin >> n >> m;

  for(int i = 1; i <= m; ++i){
  	int x, y;
  	cin >> x >> y;

  	muchii[nr++] = x;
  	muchii[nr++] = y;
 		G[x].emplace_back(nr - 1);
 		G[y].emplace_back(nr - 2);
  }

  for(int i = 1; i <= n; ++i)
  	if(G[i].size() % 2 == 1)
  			{
  				cout << -1 << '\n';
  				return 0;
  			}
  DFS(1);
  st.pop();
  if(st.size() != m){
  	cout << -1 << '\n';
  	return 0;
  }
  while(!st.empty()){
  	cout << st.top() << ' ';
  	st.pop();
  }
  return 0;
}