hdu 5444 构建二叉树,搜索二叉树
Elven Postman
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 800 Accepted Submission(s): 429
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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct BRT{
int date;
BRT *left;
BRT *right;
BRT(){}
BRT(int x):date(x){
left=NULL;
right=NULL;
}
}; void build_tree(BRT *root,int x){
if(x<root->date){
if(root->left==NULL){
root->left=new BRT(x);
return ;
}
build_tree(root->left,x);
}
else{
if(root->right==NULL){
root->right=new BRT(x);
return ;
}
build_tree(root->right,x);
}
} void get_path(BRT *root,int x){
if(root->date==x){
puts("");
return ;
}
else if(x>root->date){
printf("W");
get_path(root->right,x);
}
else{
printf("E");
get_path(root->left,x);
} } int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int x;
scanf("%d",&x);
BRT *root=new BRT(x);
for(int i=;i<n;i++){
scanf("%d",&x);
build_tree(root,x);
}
int q;
scanf("%d",&q); for(int i=;i<q;i++){
scanf("%d",&x);
get_path(root,x);
}
}
return ;
}
hdu 5444 构建二叉树,搜索二叉树的更多相关文章
- HDU 5444 Elven Postman (二叉树,暴力搜索)
题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根 ...
- 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 ...
- c++ 搜索二叉树 插入,删除,遍历操作
搜索二叉树是一种具有良好排序和查找性能的二叉树数据结构,包括多种操作,本篇只介绍插入,排序(遍历),和删除操作,重点是删除操作比较复杂,用到的例子也是本人亲自画的 用到的测试图数据例子 第一.构建节点 ...
- 【数据结构】——搜索二叉树的插入,查找和删除(递归&非递归)
一.搜索二叉树的插入,查找,删除 简单说说搜索二叉树概念: 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右 ...
- JavaScript树(二) 二叉树搜索
TypeScript方式实现源码 // 二叉树与二叉树搜索 class Node { key; left; right; constructor(key) { this.key = key; this ...
- HDU 5444 Elven Postman 二叉排序树
HDU 5444 题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼.1000个点的树,居 ...
- Lucene核心--构建Lucene搜索(上篇,理论篇)
2.1构建Lucene搜索 2.1.1 Lucene内容模型 一个文档(document)就是Lucene建立索引和搜索的原子单元,它由一个或者多个字段(field)组成,字段才是Lucene的真实内 ...
- 树&二叉树&&满二叉树&&完全二叉树&&完满二叉树
目录 树 二叉树 完美二叉树(又名满二叉树)(Perfect Binary Tree) 完全二叉树(Complete Binary Tree) 完满二叉树(Full Binary Tree) 树 名称 ...
- Python 和 Elasticsearch 构建简易搜索
Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...
随机推荐
- AD 域复制FRS 迁移到DFSR
假设您尝试将在先前版本的Windows Server上运行的某个Active Directory域控制器(DC)升级到Windows Server 2019. 您可能会看到以下错误: “副本验证失败. ...
- jspscriptlet标签
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- python基础教程总结10——文件
1.打开文件 open(name[mode[,buffing]) 参数: 文件,模式,缓冲 1)name: 是强制选项,模式和缓冲是可选的 #如果文件不在,会报下面错误1 >>&g ...
- Android(java)学习笔记117:SharedPreferences(轻量级存储类)
1.SharedPreferences是Android平台上一个轻量级的存储类,简单的说就是可以存储一些我们需要的变量信息.2个activity 之间的数据传递除了可以他通过intent来传递数据,还 ...
- 01_9_ServletContext
01_9_ServletContext 1. 例子 public void doGet(HttpServletRequest request, HttpServletResponse response ...
- dSYM文件
来到新公司后,前段时间就一直在忙,前不久 项目 终于成功发布上线了,最近就在给项目做优化,并排除一些线上软件的 bug,因为项目中使用了友盟统计,所以在友盟给出的错误信息统计中能比较方便的找出客户端异 ...
- CF-629 D - Babaei and Birthday Cake (离散化 + 线段树|树状数组)
求上升子序列的最大和.O(n^2)会暴力,在查询的时候要用线段树维护 因为权值是浮点数,故先离散化一下,设第 i 个位置的权值,从小到大排名为 id.那么dp转移中 \[d[i] = max(d[i] ...
- MySQL 自学笔记_Union(组合查询)
1. Union查询简介 组合查询:有时在使用select语句进行数据查询时,想要将多个select语句在一个查询结果中输出,此时就需要使用Union关键字. Union的使用方法:用union将多个 ...
- 201621123080《JAVA程序设计》第八周学习总结
作业08-集合 1. 本周学习总结 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 根据代码,首先在源数组里找到下标,若下标符合>=0 ...
- 20.Yii2.0框架多表关联一对多查询之hasMany
目录 新手模式 hasMany关联模式查询 新建mode层Article.php 新建mode层Category.php 新建控制器HomeController.php 新手模式 用上次的查询结果,作 ...