Cod sursa(job #641789)

Utilizator swift90Ionut Bogdanescu swift90 Data 29 noiembrie 2011 15:21:39
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.98 kb
#include<cstdio>
#include<fstream>
#include<vector>
using namespace std;
int N,M,cnt,poz;
typedef pair <int,int> hh;
vector <hh> nr[100010];
char viz[100100],fol[500100];
int Q[500100],U;
int S[500100],T;
unsigned int s[100100];
void df(){
	int x;
	viz[1]=true;
	//S.push(1);
	S[++T]=1;
	//while(!S.empty()){
	while(T){
		//x=S.top();
		x=S[T];
		while(s[x]<nr[x].size()){
			if(viz[nr[x][s[x]].first])
				++s[x];
			else
				break;
		}
		if(s[x]==nr[x].size()){
			--cnt;
			//S.pop();
			--T;
			continue;
		}
		//S.push(nr[x][s[x]].first);
		S[++T]=nr[x][s[x]].first;
		viz[nr[x][s[x]].first]=true;
		++s[x];
	}
}
void euler(){
	int x;
	//S.push(1);
	S[++T]=1;
	//while(!S.empty()){
	while(T){
		//x=S.top();
		x=S[T];
		while(s[x]<nr[x].size()){
			if(fol[ nr[x][s[x]].second ])
				++s[x];
			else
				break;
		}
		if(s[x]==nr[x].size()){
			//Q.push(x);
			Q[++U]=x;
			//S.pop();
			--T;
			continue;
		}
		fol[ nr[x][s[x]].second ]=1;
		//S.push(nr[x][s[x]].first);
		S[++T]=nr[x][s[x]].first;
		++s[x];
	}
}
void citire(){
	char S[100];
	int x,y,i,j;
	fgets(S,100,stdin);
	for(i=1;i<=M;++i){
		fgets(S,100,stdin);
		for(j=0;!('0'<=S[j] && S[j]<='9');++j)
			;
		for(x=0;'0'<=S[j] && S[j]<='9';++j)
			x=x*10+S[j]-'0';
		for(;!('0'<=S[j] && S[j]<='9');++j)
			;
		for(y=0;'0'<=S[j] && S[j]<='9';++j)
			y=y*10+S[j]-'0';
		nr[x].push_back( hh(y,i) );
		nr[y].push_back( hh(x,i) );
	}
}
int main(){
	freopen("ciclueuler.in","r",stdin);
	ofstream g("ciclueuler.out");
	int i;
	scanf("%d%d",&N,&M);
	citire();
	/*for(i=1;i<=M;++i){
		f>>x>>y;
		nr[x].push_back( hh(y,i) );
		nr[y].push_back( hh(x,i) );
	}*/
	for(i=1;i<=N;++i){
		if(nr[i].size()%2){
			g<<"-1\n";
			fclose(stdin);
			g.close();
			return 0;
		}
	}
	
	cnt=N;
	df();
	if(cnt){
		g<<"-1\n";
		fclose(stdin);
		g.close();
		return 0;
	}
	for(i=0;i<=N;++i)
		s[i]=0;
	euler();
	//Q.pop();
	//for(;!Q.empty();Q.pop())
	//	g<<Q.front()<<' ';
	for(i=2;i<=U;++i)
		g<<Q[i]<<' ';
	g<<'\n';
	fclose(stdin);
	g.close();
	return 0;
}