题意:给出一颗二叉树的先序遍历,默认的中序遍历是1.、2、……n。给出q个询问,询问从根节点出发到某个点的路径。

析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根开始搜,如果要到的结点比根结点大,那么一定是向W走,

然后去第一个结点,然后接着判定,一直走,如果找到结束就好。水题。当时想的有点复杂。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map> using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
scanf("%d", &m);
while(m--){
int u;
scanf("%d", &u);
int rt = a[0];
int s = 0;
while(true){
if(a[0] == u){ break; }
if(u > a[s]){
printf("W");
for(int i = s+1; i < n; ++i){
if(a[i] > a[s]){
s = i;
break;
}
}
if(u == a[s]) break;
}
else {
printf("E");
for(int i = s+1; i < n; ++i){
if(a[i] < a[s]){
s = i;
break;
}
}
if(u == a[s]) break;
}
}
printf("\n");
}
}
return 0;
}

HDU 5444 Elven Postman (二叉树,暴力搜索)的更多相关文章

  1. hdu 5444 Elven Postman 二叉树

    Time Limit: 1500/1000 MS (Java/Others)   Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...

  2. hdu 5444 Elven Postman

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...

  3. hdu 5444 Elven Postman(长春网路赛——平衡二叉树遍历)

    题目链接:pid=5444http://">http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limi ...

  4. 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 ...

  5. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  6. Hdu 5444 Elven Postman dfs

    Elven Postman Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  7. HDU 5444 Elven Postman 二叉排序树

    HDU 5444 题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼.1000个点的树,居 ...

  8. HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)

    Elven Postman Elves are very peculiar creatures. As we all know, they can live for a very long time ...

  9. hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online

    很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...

随机推荐

  1. C++获取进程号及窗口

    #include <TlHelp32.h> //根据进程名获取进程ID BOOL GetPidByProcessName(TCHAR *pProcess, DWORD*dwPid) { H ...

  2. UVA 11383 Golden Tiger Claw(最佳二分图完美匹配)

    题意:在一个N*N的方格中,各有一个整数w(i,j),现在要求给每行构造row(i),给每列构造col(j),使得任意w(i,j)<=row(i)+col(j),输出row(i)与col(j)之 ...

  3. MIPI DSI 和 D-PHY 初始化序列

    MIPI DSI 和 D-PHY 初始化序列 -- 深圳 南山平山村 曾剑锋 参考文档: i.MX 6Dual/6Quad Multimedia Applications Processor Refe ...

  4. 【JavaScript学习笔记】鼠标样式

    style="cursor:hand"   手形 style="cursor:crosshair"   十字形       style="cursor ...

  5. Be quiet

    Be quiet */--> UP | HOME Be quiet Table of Contents 1 Be quiet 1 Be quiet 最近心情有点不太好,各方面原因.主要是25岁是 ...

  6. 定时组件quartz系列<二>quartz的集群原理

    1.基本信息:      Quartz是一个开源的作业调度框架,它完全由java写成,并设计用于J2Se和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...

  7. 剖析OkHttp缓存机制

    现在应用市场上的 App 无一不需要网络操作,这些应用的开发者大多数都选择结合使用 OkHttp 和 Retrofit 来完成网络操作.okHttp 最为人称道的一个特性就是它的缓存机制,而我将在本篇 ...

  8. zzzzz

    Extension Method: Return another string if string is null or emptyJust a tiny little extension metho ...

  9. php常用正则

    平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2.    "^\d+$" //非负整数(正整数 + 0) 3.    "^[0-9]*[1 ...

  10. kinect 录制彩色和深度视频

    安装 KinectSDK-v1.8-Setup.exe OpenNI-Windows-x86-2.1.0.msi Qt工程 拷贝 Redist 下内容到 编译的exe所在目录 #include < ...