Cod sursa(job #1956143)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 6 aprilie 2017 15:33:25
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
# pragma GCC optimize("O3")
# include <bits/stdc++.h>
# define maxn 100001
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define pb push_back
# define mp make_pair
//# define int ll
using namespace std;

vector<pair<int,int>>vec[maxn];
int n,m,x,y;
vector<int>ans;
bool viz[maxn * 5];

int32_t main(){_
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
	cin >> n >> m;
	for(int i = 1;i <= m;i++)
	{
		cin >> x >> y;
		vec[x].pb(mp(y,i));
		vec[y].pb(mp(x,i));
	}
	for(int i = 1;i <= n;i++)
		if((int)vec[i].size() & 1) rc(-1);
	stack<int>st;
	st.push(1);
	while(!st.empty())
	{
		int u = st.top();
		if(!vec[u].empty())
		{
            auto uu = vec[u].back();
            vec[u].pop_back();
			if(!viz[uu.second])
			{
                viz[uu.second] = 1;
                st.push(uu.first);
			}
		}
		else ans.pb(u),st.pop();
	}
    for(auto it : ans) cout << it << ' ';
}