• [欧拉路径] Traveler ZOJ


    Traveler

     ZOJ - 4103 

    The famous traveler BaoBao is visiting the Dream Kingdom now. There are nn cities in Dream Kingdom, numbered from 11 to nn. The cities are connected by directed roads. For all 1 le i le n1in:

    • There is a road from the ii-th city to the (i-1)(i1)-th city if 1 le i-1 le n1i1n.
    • There is a road from the ii-th city to the 2i2i-th city if 1 le 2i le n12in.
    • There is a road from the ii-th city to the (2i+1)(2i+1)-th city if 1 le 2i+1 le n12i+1n.
    • There is a road from the ii-th city to the lfloor frac{i}{2} floor2i-th city if 1 le lfloor frac{i}{2} floor le n12in, where lfloor frac{i}{2} floor2i indicates the largest integer xx such that 2x le i2xi.

     

    BaoBao starts his travel from the 1st city. As he doesn't like visiting a city more than once, he wants to find a route which goes through each of the nn cities exactly once. Can you help him find such a route?

    Input

    There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

    The first and the only line contains an integer nn (1 le n le 10^51n105), indicating the number of cities in Dream Kingdom.

    It's guaranteed that the sum of nn of all test cases will not exceed 10^6106.

    Output

    For each test case output one line. If there exists a route which starts from the 1st city and visits each city exactly once, output nn integers c_1, c_2, dots, c_nc1,c2,,cn separated by a space, where c_ici indicates the ii-th city in the route (note that according to the description, there must be c_1 = 1c1=1). If there is no valid route, output "-1" (without quotes) instead. If there are multiple valid answers, you can output any of them.

    Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!

    Sample Input

    2
    2
    9
    

    Sample Output

    1 2
    1 3 6 5 2 4 9 8 7

    思路:

    欧拉路径,整体类似一颗有双向边的完全二叉树,但每个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个结点

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int amn=1e5+5;
     4 int n;
     5 int ans[amn],tp;
     6 int vis[amn];
     7 void sovle(int u){
     8     int f=0,flag=0;
     9     while(tp<n){
    10         vis[u]=1;
    11         ans[++tp]=u;//cout<<u<<' '<<f<<' ';
    12         if(u>=n/2)flag=1;
    13         if(!flag){
    14             if(f==0&&(u<<1|1)<=n&&!vis[u<<1|1]){
    15                 f=1;
    16                 u=(u<<1|1);
    17             }
    18             else{
    19                 f=1;
    20                 //cout<<vis[u-1]<<' '<<vis[u<<1|1];
    21                 if((u-1)>=1&&!vis[u-1]){
    22                     f=1;
    23                     u=u-1;
    24                 }
    25                 else if((u<<1|1)<=n&&!vis[u<<1|1]){
    26                     f=1;
    27                     u=(u<<1|1);
    28                 }
    29             }
    30         }
    31         else{
    32             if((u<<1|1)<=n&&!vis[u<<1|1]){
    33                 u=(u<<1|1);
    34             }
    35             else if((u<<1)<=n&&!vis[u<<1]){
    36                 u=(u<<1);
    37             }
    38             else if((u>>1)>=1&&!vis[u>>1]){
    39                 u=(u>>1);
    40             }
    41             else if((u-1)>=1&&!vis[u-1]){
    42                 u=u-1;
    43             }
    44         }
    45         //cout<<endl;
    46     }
    47 }
    48 int main(){
    49     int T;cin>>T;
    50     while(T--){
    51         cin>>n;
    52         memset(vis,0,sizeof vis);
    53         tp=0;
    54         sovle(1);
    55         for(int i=1;i<=n;i++){
    56             printf("%d%c",ans[i],i<n?' ':'
    ');
    57         }
    58     }
    59 }
    60 /**
    61 欧拉路径,整体类似一颗有双向边的完全二叉树,但每个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个结点
    62 */
  • 相关阅读:
    JS日期格式转换
    VMware虚拟化集群的配置(一)
    网络初级篇之STP(概念原理)
    运维学习篇之jenkins的安装(CentOS7)
    网络初级篇之配置telnet登录网络设备(实验)
    Linux操作篇之OpenKM的安装(汉化)
    网络初级篇之网络设备的FTP(原理与实验)
    网络初级篇之直连路由与静态路由(原理与实验)
    网络初级篇之DHCP原理与配置(原理与实验)
    网络初级篇之OSPF(二)实验
  • 原文地址:https://www.cnblogs.com/Railgun000/p/12820353.html
Copyright © 2020-2023  润新知