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中的项.这样的二叉树是二叉查找树. 以上是一颗二叉查找树,其 ...
随机推荐
- UVa 10720 - Graph Construction(Havel-Hakimi定理)
题目链接: 传送门 Graph Construction Time Limit: 3000MS Memory Limit: 65536K Description Graph is a coll ...
- Linux 之 编译器 gcc/g++参数详解
2016年12月9日16:48:53 ----------------------------- 内容目录: [介绍] gcc and g++分别是gnu的c & c++编译器 gcc/g++ ...
- 网络存储(四)之ISCSI的进阶
前言 上一篇博客中我们讲了如何搭建一个简单的iscsi网络存储系统,这块有个安全问题就是,任何知道target name的客户端都可以随意连接ISCSI服务器.但是很多时候,通过授权认证连接共享磁盘或 ...
- jsp分页功能
http://blog.csdn.net/xiazdong/article/details/6857515
- 【原】js 签到用日历
最近做的一个项目中,需要用到一个日历来记录你的签到,网上找了一些,感觉挺庞大的,所以就自己写了一个,记录一下自己写这个日历的经过 html代码: <table cellspacing=" ...
- 用arp-scan扫描局域网IP地址
1,在安装之前需要安装yum install -y libpcap libpcap-devel如果没有安装yum工具需要用rpm安装如下软件包[root@oradba arp-scan-1.8]# y ...
- 最好用的placeholder插件,jQuery插件EnPlaceholder
EnPlaceholder插件支持密码框哦!实际对比同类的placeholder插件在ie等浏览器下效果做好! 插件效果预览:http://www.wufangbo.com/demo/jquery/3 ...
- Mac安装Brew
安装命令如下:curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip ...
- cdn提供商
七牛,又拍 http://www.qiniu.com/ https://www.upyun.com/index.html
- 在 docker中 运行 mono /jexus server 并部署asp.net mvc站点
http://linuxdot.net/bbsfile-3988 1. 安装 docker: // docker 1.7 新版 安装非常容易,理论上说,在主流的任意linux发行版上都可以 ...