zoj 3965 Binary Tree Restoring(搜索)
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 a1, a2, ..., an (1 ≤ ai ≤ n, ∀ 1 ≤ i < j ≤ n, ai ≠ aj), indicating the first DFS sequence of the binary tree.
The third line of each test case contains n integers b1, b2, ..., bn (1 ≤ bi ≤ n, ∀ 1 ≤ i < j ≤ n, bi ≠ 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(搜索)的更多相关文章
- ZOJ 3965 Binary Tree Restoring
Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...
- 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...
- [LeetCode] Balanced Binary Tree 深度搜索
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- ZOJ3965 Binary Tree Restoring
ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [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 ...
随机推荐
- PAT 1073 多选题常见计分法 (20 分)
批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数:如果考生选择了任何一个错误的选项,则不能得分.本 ...
- HDU1950-Bridging signals-最长上升子序列
Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. ...
- C# 函数4
//数据库 public class GF_DA { /// <summary> /// 执行SQL语句 sConnStr 连接字符串,sq ...
- linux各个文件夹作用
linux /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/ho ...
- C++实现计算器功能(包括计算含未知量的式子),输出后缀表达式
大概描述 用c++语言在vc中实现部分数学计算功能.其中实现的数学计算功能包括加减乘除运算.开方计算.自然对数运算.以10为底的对数运算.幂计算.正弦余弦计算. 由用户输入要计算的表达式 ...
- C#如何使用结构化异常处理
Knowledge Base: Chinese (Simplified) 如何使用 Visual C# .NET 和 Visual C# 2005 中的结构化异常处理文章ID: 816157 最近更新 ...
- ajax使用formdata 提交excel文件表单到rails解析
.modal-body .container-fluid .row .col-md-12 1.下载模板文件 = link_to '模板文件' .row .col-md-12 = form_tag '' ...
- vuex 入门
vuex.js 状态(数据)管理 在vue中当我们管理数据的时候比较乱,我们要用到下面的这个库,vuex.js Vuex介绍 每一个Vuex应用的核心就是store(仓库),他是用来存储数据的 &qu ...
- HTML5(。。。。不完整)
<!DOCTYPE html> 不区分大小写 <header>.<nav>.<article>.<section>.<sidebar ...
- C#多线程学习之Thread
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...