(This article only keeps notes, please go to see the official)
For this example, I am puzzled by Euler's path, let's look at the program flow and general idea for the time being.
#include<bits/stdc++.h>
using namespace std;
const int amn=1e5+5;
int n;
int ans[amn],tp;
int vis[amn];
void sovle(int u){
int f=0,flag=0;
while(tp<n){
vis[u]=1;
ans[++tp]=u;//cout<<u<<' '<<f<<' ';
if(u>=n/2)flag=1;
if(!flag){
if(f==0&&(u<<1|1)<=n&&!vis[u<<1|1]){
f=1;
u=(u<<1|1);
}
else{
f=1;
//cout<<vis[u-1]<<' '<<vis[u<<1|1];
if((u-1)>=1&&!vis[u-1]){
f=1;
u=u-1;
}
else if((u<<1|1)<=n&&!vis[u<<1|1]){
f=1;
u=(u<<1|1);
}
}
}
else{
if((u<<1|1)<=n&&!vis[u<<1|1]){
u=(u<<1|1);
}
else if((u<<1)<=n&&!vis[u<<1]){
u=(u<<1);
}
else if((u>>1)>=1&&!vis[u>>1]){
u=(u>>1);
}
else if((u-1)>=1&&!vis[u-1]){
u=u-1;
}
}
//cout<<endl;
}
}
int main(){
int T;cin>>T;
while(T--){
cin>>n;
memset(vis,0,sizeof vis);
tp=0;
sovle(1);
for(int i=1;i<=n;i++){
printf("%d%c",ans[i],i<n?' ':'\n');
}
}
}
/**
欧拉路径,整体类似一颗有双向边的完全二叉树,但每个u(u>1)都有一条指向u-1的有向边,u<n/2时走一次2*u+1,然后走完u-1,如此循环,当u==n/2之后,按优先级走,优先2*u+1,其次2*u,再次u/2,最次u-1,如此循环,直到走完n个结点
*/
Take n as 21 as an example. The whole is similar to a complete binary tree with two-way edges, but each u (u>1) has a directed edge pointing to u-1. The current cities are u, when u<n/2, walk 2 u+1 once , and then complete u-1, and then loop through it. At the same time, if u>=n/2 is encountered during the loop and the loop is stopped, the following priority Steps go, first 2 u+1, second 2*u, finally u/2, and so on, until n cities are completed.
Note, remember to see the series of knowledge points of Euler's path.