Elven Postman(BST )
Elven Postman
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 147 Accepted Submission(s): 90
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.
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.
Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
WE
EEEEE
#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int M = 1e3 + 10 ;
pair<int,int> g[M] ;
int n , x ; void BST (int u) {
if (x < u) {
if (g[u].F == -1) {
g[u].F = x ;
return ;
}
BST (g[u].F) ;
}
else {
if (g[u].S == -1) {
g[u].S = x ;
return ;
}
BST (g[u].S) ;
}
} void dfs (int u) {
if (x == u) {
puts ("") ;
return ;
}
if (x < u) {
printf ("E") ;
dfs (g[u].F) ;
}
else {
printf ("W") ;
dfs (g[u].S) ;
}
} int main () {
int T ;
scanf ("%d" , &T ) ;
while (T --) {
scanf ("%d" , &n) ;
int root ;
scanf ("%d" , &root) ;
for (int i = 1 ; i <= n ; i ++) g[i] = {-1,-1} ;
for (int i = 2 ; i <= n ; i ++) {
scanf ("%d" , &x) ;
BST (root) ;
}
int q ;
scanf ("%d" , &q) ;
while (q --) {
scanf("%d" , &x) ;
dfs (root) ;
}
}
return 0 ;
}
Elven Postman(BST )的更多相关文章
- Elven Postman(二叉树)
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 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 ...
- 【转载】图解:二叉搜索树算法(BST)
原文:图解:二叉搜索树算法(BST) 摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!“岁月极美,在于它必然的流逝”“春花 秋月 夏日 冬雪”— ...
- C++数据结构之二叉查找树(BST)
C++数据结构之二叉查找树(BST) 二分查找法在算法家族大类中属于“分治法”,二分查找的过程比较简单,代码见我的另一篇日志,戳这里!因二分查找所涉及的有序表是一个向量,若有插入和删除结点的操作,则维 ...
- 【算法】二叉查找树(BST)实现字典API
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- postman(一)批量执行接口测试用例
postman(一)批量执行接口测试用例 学习了:https://blog.csdn.net/github_36032947/article/details/78611405 还可以把collecti ...
- 数据结构------------------二叉查找树(BST)的java实现
数据结构------------------二叉查找树(BST)的java实现 二叉查找树(BST)是一种能够将链表插入的灵活性和有序数组查找的高效性相结合的一种数据结构.它的定义如下: 二叉查找树是 ...
- 二叉查找树(BST)
二叉查找树(BST) 二叉查找树(Binary Search Tree)又叫二叉排序树(Binary Sort Tree),它是一种数据结构,支持多种动态集合操作,如 Search.Insert.De ...
- 【数据结构】什么是二叉查找树(BST)
什么是二叉查找树(BST) 1. 什么是BST 对于二叉树中的每个节点X,它的左子树中所有项的值都小于X中的项,它的右子树中所有项的值大于X中的项.这样的二叉树是二叉查找树. 以上是一颗二叉查找树,其 ...
随机推荐
- 【Alpha版本】 第十天 11.18
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成管理员的三大功能界面框架, 我要招聘查看报名者的列表显示 完成 ...
- UVALive 3989Ladies' Choice(稳定婚姻问题)
题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...
- python 培训之Django
1.Install sudo apt-get install python-pip sudo pip install django==1.8 2. Create Project django- ...
- Saltstack远程执行(四)
Saltstack远程执行 语法例:salt '*' cmd.run 'w' - 命令:salt - 目标:'*' - 模块:cmd.run,自带150+模块,也可以自己写模块 - 返回:执行 ...
- Win10微软官方最终正式版ISO镜像文件
Win10微软官方最终正式版ISO镜像文件 据说Windows 10是微软发布的最后一个Windows版本,下一代Windows将作为Update形式出现.Windows 10将发布7个发行版本,分别 ...
- OC面向对象特性:封装
概念性知识 1.c语言是面向过程编程:分析解决问题的步骤,实现函数,依次调用 2.oc语言是面向对象编程:分析问题的组成的对象,协调对象间的联系和通信,解决问题 3.#include和#impo ...
- 按照网上方法js删除指定cookie,却怎么也删除不了,解决如下
网上方法: 查找原因说是没有指定Path,记得系统里以前也没指定还是可以的,就查了一下现在的系统Path,猜测是系统Path由以前的/改为/E7-Planning 就改了前端删除方法 测试一下OK了, ...
- Java多线程系列
一.参考文献 1.:Java多线程系列目录 (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 03. ...
- python 传递结构体指针到 c++ dll
CMakeLists.txt # project(工程名) project(xxx) # add_library(链接库名称 SHARED 链接库代码) add_library(xxx SHARED ...
- 3. 量化交易策略 - https://github.com/3123958139/blog-3123958139/README.md
3. 量化交易策略 * 输入数据 - 只取最原始可靠的,如 * date * open * high * low * close * volume * 输出数据 - 根据数理统计取权重,把 o, h, ...