• hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online


    Problem Description
    Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something to be taken lightly. Also, they live on trees. However, there is something about them you may not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more “traditional” methods.

    So, as a elven postman, it is crucial to understand how to deliver the mail to the correct room of the tree. The elven tree always branches into no more than two paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only that, when numbering the rooms, they always number the room number from the east-most position to the west. For rooms in the east are usually more preferable and more expensive due to they having the privilege to see the sunrise, which matters a lot in elven culture.

    Anyways, the elves usually wrote down all the rooms in a sequence at the root of the tree so that the postman may know how to deliver the mail. The sequence is written as follows, it will go straight to visit the east-most room and write down every room it encountered along the way. After the first room is reached, it will then go to the next unvisited east-most room, writing down every unvisited room on the way as well until all rooms are visited.

    Your task is to determine how to reach a certain room given the sequence written on the root.

    For instance, the sequence 2, 1, 4, 3 would be written on the root of the following tree.

     
    Input
    First you are given an integer T(T10) indicating the number of test cases.

    For each test case, there is a number n(n1000) on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively a1,...,an where a1,...,an{1,...,n}.

    On the next line, there is a number q representing the number of mails to be sent. After that, there will be q integers x1,...,xq indicating the destination room number of each mail.
     
    Output
    For each query, output a sequence of move (E or W) the postman needs to make to deliver the mail. For that E means that the postman should move up the eastern branch and W the western one. If the destination is on the root, just output a blank line would suffice.

    Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
     
    Sample Input
    2
    4
    2 1 4 3
    3
    1 2 3
    6
    6 5 4 3 2 1
    1
    1
     
    Sample Output
    E
     
    WE
    EEEEE
     
    Source
     

    考点明确,二叉树。先序遍历的顺序建立二叉树。
      1 #include<stdio.h>
      2 #include<iostream>
      3 #include<string>
      4 #include<cstring>
      5 #include<algorithm>
      6 #include<queue>
      7 
      8 using namespace std;
      9 
     10 char ans[1100][1100];
     11 
     12 struct Node
     13 {
     14     int num,we[2];    //分别代表当前节点的值,左孩子,右孩子的序号 
     15     Node(int num=0):num(num){
     16         we[0]=we[1]=0;
     17     }    
     18 };
     19 
     20 struct Tree
     21 {
     22     int n;
     23     
     24     Node node[11000];
     25     
     26     void init()
     27     {
     28         n=0;
     29     }
     30     
     31     int newNode(int val)
     32     {
     33         node[++n]=Node(val);
     34         return n;
     35     }
     36     
     37     void insert(int &u,int val)
     38     {
     39         if(u==0)
     40         {
     41             u=newNode(val);
     42             return;
     43         }
     44         
     45         if(val<node[u].num)
     46         {
     47             insert(node[u].we[0],val);
     48         }
     49         else
     50         {
     51             insert(node[u].we[1],val);
     52         }
     53     }
     54     
     55     void solve(int u,int d,char* s)
     56     {
     57         if(u==0) return;
     58         s[d]='';
     59         strcpy(ans[node[u].num],s);
     60         
     61         if(node[u].we[0])
     62         {
     63             s[d]='E';
     64             solve(node[u].we[0],d+1,s);
     65         }
     66         if(node[u].we[1])
     67         {
     68             s[d]='W';
     69             solve(node[u].we[1],d+1,s);
     70         }
     71     }
     72     
     73 }solver;
     74 
     75 int main()
     76 {
     77     int k,m,q,t,p;
     78     int T;
     79     
     80     cin>>T;
     81     
     82     while(T--)
     83     {
     84         int n;
     85         
     86         cin>>n;
     87         
     88         if(n==0) continue;
     89         
     90         cin>>k;
     91         int root = solver.newNode(k);
     92         
     93         for(int i=2;i<=n;++i)
     94         {
     95             cin>>k;
     96             solver.insert(root,k);
     97         }
     98         
     99         char s[1100];
    100         solver.solve(root,0,s);
    101         
    102         cin>>m;
    103         while(m--)
    104         {
    105             cin>>t;
    106             cout<<ans[t]<<endl;
    107         }
    108          
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    树链剖分( 洛谷P3384 )
    ZJOI 2015 诸神眷顾的幻想乡
    BZOJ 1002 [FJOI2007]轮状病毒
    洛谷 P1485 火枪打怪
    Luogu2860 [USACO06JAN]冗余路径Redundant Paths
    CF962F Simple Cycles Edges
    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
    Luogu2295 MICE
    CF341D Iahub and Xors
    CF617E XOR and Favorite Number
  • 原文地址:https://www.cnblogs.com/Traveller-Leon/p/4853701.html
Copyright © 2020-2023  润新知