链表实现的图和树的遍历的算法,利用C语言的程序的设计来实现

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(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,...,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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

3
数据结构C语言链表输入排序问题
4
交换二叉树中每个结点的左孩子和右孩子C++语言
3
线索二叉树,怎么可能把二叉树变为双向链表呢?
4
C语言链表与文件 从文件读数据到链表里
3
Java该怎么遍历链表数据结构
1
C语言链表undefined reference to 问题
2
c++实现单链表的在指定元素(x)前插入元素(b)的算法
2
c语言中两个链表的合并(不用结点的方式)总是错误的
0
c++基于链表储存的图的相关算法的验证
1
c语言链表问题,希望能在10号之前得到解答
2
C语言链表插入问题 插入节点到头节点之前去 遍历后发现只能显示插入的那个节点。
2
十字交叉双向循环链表如何解决这个作为匹配的算法,怎么C语言实现的呢?
0
使用循环链表实现一个双向队列并且实现出队列和进队列的编程算法, 使用C语言的编程?
0
采用循环队列的链表实现的方式的环路的设计,数据结构C语言习题的思路
0
二叉树的链表存储,线索二叉树的算法问题,怎么使用C语言实现节点遍历
0
用链表法生成动态列表送并且实现链表的排序的算法和查询,运用C语言编程的知识
0
一个全链表的搜索的问题,符号的识别的思路,采用C语言的算法
0
链表计算多项式的一个具体算法问题的实现,数据结构采用C语言的办法
1
C语言求助:输入一个字符串,将其中的字母字符输入一个链表,将其中的数字字符输入另一个链表。
0
循环链表的旋转问题,怎么实现这里的位置转换,用C语言程序设计的办法