首页 > 代码库 > HDU 2894 欧拉回路

HDU 2894 欧拉回路

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 20020
struct node{
	int from, to, dou, nex;
}edge[N];
int head[N], edgenum;
void add(int u, int v,int dou){
	node E={u,v,dou,head[u]};
	edge[edgenum] = E;
	head[u] = edgenum++;
}
void init(){memset(head, -1, sizeof head); edgenum = 0;}
bool vis[10000];
int n;
int Stack[N], top;
void dfs(int u){
	for(int i = head[u]; ~i; i = edge[i].nex){
		int dou = edge[i].dou, v = edge[i].to; if(vis[dou])continue;
		vis[dou] = 1;
		dfs(v);
		Stack[top++] = dou;
	}
}
int main(){
	int i, j;
	while(~scanf("%d",&n)){
		init();
		top = 0;
		int all = 1<<(n-1); all--;
		for(i = 0; i <= all; i++){
			int dou = i<<1|1;
			int v = dou&all;
			add(i,v,dou);
			dou = i<<1;
			v = dou&all;
			add(i,v,dou);
		}
		memset(vis, 0, sizeof vis);
		dfs(0);
		printf("%d ",1<<n);
		for(i = top-1; i >= 0; i--){
			printf("%d", Stack[i]/(all+1));
			if(!i)puts("");
		}
	}
	return 0;
}