Binary Tree Restoring


Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

Given two depth-first-search (DFS) sequences of a binary tree, can you find a binary tree which satisfies both of the DFS sequences?

Recall that a binary tree is a tree in which each vertex has at most two children, and the depth-first search is a tree traversing method which starts at the root and explores as far as possible along each branch before backtracking.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 105), indicating the number of vertices in the binary tree.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ n, ∀ 1 ≤ i < j ≤ nai ≠ aj), indicating the first DFS sequence of the binary tree.

The third line of each test case contains n integers b1b2, ..., bn (1 ≤ bi ≤ n, ∀ 1 ≤ i < j ≤ nbi ≠ bj), indicating the second DFS sequence of the binary tree.

It is guaranteed that the sum of n over all test cases does not exceed 106, and there always exists at least one possible binary tree.

We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.

Output

For each test case, output one line which contains n integers seperated by one space. The i-th integer indicates the father of the i-th vertex in the binary tree which satisfies both of the DFS sequence. If the i-th vertex is the root of the binary tree, output 0 as its father. If there are multiple valid answers, you can output any of them.

Please, DO NOT print extra spaces at the end of each line, or your program may get a "wrong answer" verdict as this problem is special judged.

Sample Input

2
6
3 4 2 5 1 6
3 4 5 2 1 6
3
1 2 3
1 2 3

Sample Output

3 4 0 3 4 1
0 1 2

Author: WENG, Caizhi

给定二组序列,分别为同一个二叉树的 DFS 序列(序列不同在于对于有两个子节点 x, y 的父节点 p ,DFS 按随机选择优先访问 x 或 y )。

解题思路:

对 DFS 结果序列进行模拟,可以类似的参考树的前、中、后序互相转换的递归思路。

当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个儿子。如果子树中还有未连根的点则接到当前点下。son数组表示每个点的子树有多少个点。pos数组记录每个数在每个序列中的位置。dfs中p1,p2指向同一个数

lim1,lim2表示当前点子树可能最大的子树范围。

用样例或者更复杂的串可以模拟一下,这样比较简单:如例:3 4 2 5 7 1 6和3 1 6 4 5 7 2

用深搜的方式不断分割左子树和右子树!!!

 #include<bits/stdc++.h>
using namespace std;
const int N = + ;
int a[N], b[N]; /// 序列 A, B
int posa[N], posb[N]; /// 节点值 i 在序列 A(B) 中的位置 哈希表
int par[N]; /// 记录节点值 i 的父节点值
int pnt; /// pnt-1 表示当前已知道了多少个节点的父节点 -> 即 pnt 为之后需处理第 pnt 个点的父节点
void solve(int pos,int l,int r,int fa){ ///当前需处理 A[pos] 的父节点,对应 B 序列的区间为 [l,r]
if(l>r) return;
if(a[pos]==b[l]){///如果当前要确定的节点和第二个串第一个字母是一样的
par[a[pos]]=fa;
pnt++;
solve(pos+,l+,r,a[pos]);
if(pnt-pos<=r-l){
int tmp=pnt;
par[a[tmp]]=fa;
pnt++;
solve(tmp+,l+tmp-pos+,r,a[tmp]);
}
}
else{
par[a[pos]]=fa;
pnt++;
int len1=posa[b[l]]-pos-;
solve(pos+,posb[a[pos]]+,posb[a[pos]]+len1,a[pos]);
par[b[l]]=fa;
pnt++;
solve(posa[b[l]]+,l+,posb[a[pos]]-,b[l]);
}
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&a[i]),posa[a[i]]=i; for(int i=;i<=n;++i)
scanf("%d",&b[i]),posb[b[i]]=i; pnt=;
solve(,,n,);
for(int i=;i<=n;++i){
if(i>) printf(" ");
printf("%d",par[i]);
}
printf("\n");
}
}

zoj 3965 Binary Tree Restoring(搜索)的更多相关文章

  1. ZOJ 3965 Binary Tree Restoring

    Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...

  2. 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...

  3. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. ZOJ3965 Binary Tree Restoring

    ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...

  5. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  7. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. 超详细:CSS-float详解

    Float 详解 本文摘自:http://www.cnblogs.com/yuanchenqi/articles/5615774.html 首先要知道,div是块级元素,在页面中独占一行,自上而下排列 ...

  2. python模块学习(四)

    re模块 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C ...

  3. 异常处理、socket基于TCP协议编程

    一.异常处理 1.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误过不了Python解释器的语法检测,必须在程序执行前改正) #语法错误示范一 if #语法错误示范二 de ...

  4. Linux中的grep和cut

    提取行: grep --color  着色 -v         不包含 提取列: cut -f      列号 提取第几列 -d     分隔符 以什么为分隔符,默认是制表键 局限性:如果分隔符不那 ...

  5. spring mvc 关键接口 HandlerMapping HandlerAdapter

    HandlerMapping Spring mvc 使用HandlerMapping来找到并保存url请求和处理函数间的mapping关系.   以DefaultAnnotationHandlerMa ...

  6. Spring:笔记整理(2)——IOC容器

    IOC容器 什么是IOC 说明 IOC ,全称Inversion of control,即,控制反转,是一种设计思想. 控制: 在Java中,IOC意味着:你将设计好的对象交给容器控制,而不是传统的在 ...

  7. Android sdk manager加载缓慢或加载不出来

    1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...

  8. HP小型机维护

    (一)文件系统维护 . 监控文件系统的使用 # bdf . 监控文件目录的使用 # du -sk /myfs2/* (二)网络系统维护 1. 相关配置文件 1). 主机名定义文件:/etc/hosts ...

  9. 【Topcoder】SRM158 DIV2总结

    250分题:给定一个4位字符串initial和rotate这个字符串的方式,然后再给另一个字符串current,问current能否由initial通过rotate得到,需要几次rotate? 简单的 ...

  10. JSP笔记03——环境搭建(转)

    不完全翻译,结合谷歌,一定主观性,还可能有误,原始内容地址:https://www.tutorialspoint.com/jsp/jsp_environment_setup.htm [注释]这篇貌似有 ...