版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/82382476
之前似乎在poj做过一道一模一样的,但是我是直接dfs暴力跑的,因为学了Fleury,所以用这个做了一遍,我们把每个数字抽象为边,点为一个n-1位的二进制数,然后做一个欧拉回路,跑有向图和无向图是一样的,没什么区别
#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#define maxn 17005
#define maxx
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int head[maxn],_next[maxn<<1],to[maxn<<1];
int edge=1;
void addEdge(int x,int y)
{
to[++edge]=y,_next[edge]=head[x],head[x]=edge;
}
stack<int> st;
bool vis[maxn<<1];
void dfs(int u)
{
for(int i=head[u];i;i=_next[i])
{
if(vis[i])continue;
vis[i]=true;
st.push(to[i]);
dfs(to[i]);
break;
}
}
int ans[maxn<<1],cnt;
void fleury(int r)
{
st.push(r);
while(st.size())
{
int u=st.top();
bool sign=true;
for(int i=head[u];i;i=_next[i])
{
if(vis[i])continue;
sign=false;
vis[i]=true;
st.push(to[i]);
dfs(to[i]);
break;
}
if(sign)
{
ans[++cnt]=(st.top()&1);
st.pop();
}
}
}
int main()
{
int n;
cin>>n;
if(n==1)
{
cout<<01<<endl;
}
else
{
int _n=n-1;
int limit=1<<_n;
for(int s=0;s<limit;s++)
{
int _s=s;
if(_s&(1<<(_n-1)))_s^=1<<(_n-1);
_s<<=1;addEdge(s,_s);
_s|=1;addEdge(s,_s);
}
fleury(0);
for(int i=cnt;i>=1;i--)
cout<<ans[i];
}
return 0;
}