Cod sursa(job #2404314)

Utilizator MaarcellKurt Godel Maarcell Data 12 aprilie 2019 15:37:36
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.76 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <ctime>
#include <unordered_map>
#include <iomanip>
#include <complex>
#include <cassert>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define deb(a) cerr<< #a << "= " << (a)<<"\n";

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
typedef complex<double> base;
typedef vector<int> vi;
typedef pair<int,int> pii;

template<class T> ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream; }
ll fpow(ll x, ll p, ll m){ll r=1; for (;p;p>>=1){ if (p&1) r=r*x%m; x=x*x%m; } return r;}
ll inv(ll a, ll b){ return a<2 ? a : ((a-inv(b%a,a))*b+1)/a%b; }
int gcd(int a, int b){ if (!b) return a; return gcd(b,a%b);}
ll gcd(ll a, ll b){ if (!b) return a; return gcd(b,a%b);}


struct edge{
	int to,id;
};


int N,M,ptr[100100],st[500100],K,sol[500100],T; bool v[500100];
vector<edge> g[100100];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	ifstream cin("ciclueuler.in");
	ofstream cout("ciclueuler.out");
	
	cin >> N >> M;
	
	int i;
	for (i=1; i<=M; i++){
		int x,y;
		cin >> x >> y;
		g[x].pb({y,i});
		g[y].pb({x,i});
	}
	
	st[++K]=1;
	
	while (K>0){
		int nod=st[K];

			
		bool f=0;
		for (;ptr[nod]<(int)g[nod].size(); ptr[nod]++){
			edge e = g[nod][ptr[nod]];
			if (v[e.id]) continue;
			
			st[++K]=e.to;
			v[e.id]=1;
			f=1;
			break;
		}
		
		if (!f){
			sol[++T]=nod;
			K--;
		}
	}
	
	if (T-1!=M){
		cout << -1 << "\n";
		return 0;
	}
	
	for (i=1; i<=T-1; i++)
		cout << sol[i] << " ";
}