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 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...
随机推荐
- Python3中requests库学习01(常见请求示例)
1.请求携带参数的方式1.带数据的post data=字典对象2.带header的post headers=字典对象3.带json的post json=json对象4.带参数的post params= ...
- 洛谷 P2380 狗哥采矿
题目背景 又是一节平静的语文课 狗哥闲来无事,出来了这么一道题 题目描述 一个n*m的矩阵中,每个格子内有两种矿yeyenum和bloggium,并且知道它们在每个格子内的数量是多少.最北边有blog ...
- 在windows 上编译部署Rap2
在windows 上编译部署Rap2 引言 安装需要的环境 安装后端站点 创建数据库 在全局安装pm2 和 typescript 配置mysql,redis 数据库链接配置 初始化 编译 初始化数据库 ...
- cesium-大规模人群运动测试
环境:cesium1.57: 笔记本电脑:集成显卡+独显Navida 1060 测试内容:大规模人群运动(500人,可设置运动的路径),可行性及帧率 测试结果:21-23FPS,较为流畅:集显70%- ...
- MVC批量上传文件(使用uploadify)
<script src="JS/jquery-1.8.3.js"></script> <script src="uploadify/jque ...
- typescript设置全屏
fullScreen() { document.getElementById("fullScreen").style.display = "none"; doc ...
- Dojo的define接口
http://blog.csdn.net/lovecarpenter/article/details/53979717 第三种用法用的最多. 此接口用于定义模块: define([],function ...
- 自行实现一个简易RPC框架
10分钟写一个RPC框架 1.RpcFramework package com.alibaba.study.rpc.framework; import java.io.ObjectInputStrea ...
- 解决mysql出现的问题#1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i
最近在学flask, 在访问主页时,一直出现1055错误,在网上找的解决方法是删除ONLY_FULL_GROUP_BY,当时是删除了,但是退出在进行select @@sql_mode时,仍出现ONLY ...
- 《linux设备驱动开发详解》笔记——18 ARM linux设备树
18.1 设备树的起源 linux 2.6及之前,大量板级信息被硬编码到内核里,十分庞大,大量冗余代码: linux 2.6之前,引入了设备树: 设备树源于OpenFirmware,描述硬件的数据结构 ...