Hdu 5444 Elven Postman (Changchun cyber competition-balanced binary tree traversal)

Source: Internet
Author: User

Hdu 5444 Elven Postman (Changchun cyber competition-balanced binary tree traversal)
Elven PostmanTime Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission (s): 1206 Accepted Submission (s): 681

Problem DescriptionElves 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 ction 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 wocould be written on the root of the following tree.


Input First you are given an integer T (T ≤ 10) Indicating the number of test cases.

For each test case, there is a number N (n = 1000) On a line representing the number of rooms in this tree. N Integers representing the sequence written at the root follow, respectively A1,..., 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 shocould move up the eastern branch and W The western one. If the destination is on the root, just output a blank line wowould 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 

Sample Output
E WE EEEEE 

Source 2015 ACM/ICPC Asia Regional Changchun Online
Recommend hujie | We have carefully selected several similar problems for you: 56645663566256615660
Give n nodes. The first input is to give a root node, and the following nodes are smaller than the root node on the left, A node larger than the root node is attached to the right of the root node. And so on ....

Q is given to ask the path from each root node to a certain point.

Solution: just add one point at a time, compare it with the root node, and finally traverse the binary tree for search.

For details, see the code.

#include 
 
  #include 
  
   using namespace std;struct node{    int data;    node *leftchild,*rightchild;    node (int st)    {        data=st;        leftchild=rightchild=NULL;    }};void creat(node *root,int num){    if (root->data>num)    {        if (root->leftchild==NULL)        {            node *n=new node(num);            root->leftchild=n;        }        else        {            creat(root->leftchild,num);        }    }    else    {        if (root->rightchild==NULL)        {            node *n=new node(num);            root->rightchild=n;        }        else        {            creat(root->rightchild,num);        }    }}void get_path(node *root,int x){    if (root->data==x)    {        puts("");        return ;    }    else if (root->data>x)    {        putchar('E');        get_path(root->leftchild,x);    }    else    {        putchar('W');        get_path(root->rightchild,x);    }}int main(){    int t;    int a;    scanf("%d",&t);    while (t--)    {        int n;        scanf("%d",&n);        int st;        scanf("%d",&st);        node *root=new node(st);        for (int i=1;i
   
    

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.