HDU 5444 Elven Postman 二叉排序树
题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼)
思路:这他妈又是个大水题,可是我还是太傻逼。1000个点的树,居然用标准二叉树结构来存点,,,卧槽想些什么东西。可以用一维数组,left,right直接指向位置就行了,我这里用的是链表。对于读入的序列,生成一个二叉排序树,同时记录一下路径就可以了,后面居然忘了清空path又wa了一发。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define inf 0x3f3f3f3f
#define LL long long
#define MAXN 2005
#define IN freopen("in.txt","r",stdin);
using namespace std; struct Node{
int x;
Node *left, *right;
Node(int x = ) :x(x){
left = NULL;
right = NULL;
};
}; vector<char> path[MAXN]; Node *root;
void bst_insert(int x){
//Node *t = root;
//return;
Node *t = root;
//Node *s = &Node(x);
while (t != NULL){
if (x > t->x){
path[x].push_back('W');
if (t->right == NULL){
t->right = new Node(x);
break;
}
else{
t = t->right;
}
}
else{
path[x].push_back('E');
if (t->left == NULL){
t->left = new Node(x);
break;
}
else{
t = t->left;
}
}
}
} int main(){
//IN;
//freopen("out.txt", "w", stdout);
int t, n;
scanf("%d", &t);
while (t--){
scanf("%d", &n);
int x;
scanf("%d", &x);
root = new Node(x);
for (int i = ; i <= n; i++){
path[i].clear();
}
for (int i = ; i <= n; i++) {
scanf("%d", &x);
bst_insert(x);
}
// find(1, n, 1);
int q;
scanf("%d", &q);
//bfs();
while (q--){
scanf("%d", &x);
for (int i = ; i < path[x].size(); i++){
printf("%c", path[x][i]);
}
printf("\n");
}
}
return ;
}
HDU 5444 Elven Postman 二叉排序树的更多相关文章
- hdu 5444 Elven Postman
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Description Elves are very peculia ...
- hdu 5444 Elven Postman(长春网路赛——平衡二叉树遍历)
题目链接:pid=5444http://">http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limi ...
- 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 ...
- Hdu 5444 Elven Postman dfs
Elven Postman Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 5444 Elven Postman 二叉树
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descrip ...
- 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 ...
- 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 ...
- hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online
很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
- HDU 5444 Elven Postman (二叉树,暴力搜索)
题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根 ...
随机推荐
- 入门python:《Python编程快速上手让繁琐工作自动化》中英文PDF+代码
入门推荐学习<python编程快速上手>前6章是python的基础知识,通俗易懂地讲解基础,初学者容易犯错的地方,都会指出来.从第三章开始,每章都有一个实践项目,用来巩固前面所学的知识. ...
- python 比较数字大小按从大到小输出
主要用到的python 的知识点 1: 内置函数max 2: 列表的操作 3: while 循环 4 : 错误处理 代码如下: #!/usr/bin/python #coding=u ...
- pandas 4 处理缺失数据nan
from __future__ import print_function import pandas as pd import numpy as np np.random.seed(1) dates ...
- caioj 1161 欧拉函数3:可见点数
(x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...
- XXXfragment that is not a fragment错误,fragment认不出来
要注意的是fragment事实上是有两个版本号的,一个是 import android.support.v4.app.Fragment; 另外一个是 import android.app.Fragme ...
- 后缀自己主动机(SAM)学习指南
*在学习后缀自己主动机之前须要熟练掌握WA自己主动机.RE自己主动机与TLE自己主动机* 什么是后缀自己主动机 后缀自己主动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂 ...
- html+css实现选项卡功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- poj_2828线段树,逆序插入
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- android常用控件的使用方法
引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...
- ORM中基于对象查询与基于queryset查询
感谢老男孩~ 一步一步走下去 前面是视图函数 后面是表结构models.py from django.shortcuts import render, HttpResponse from djang ...