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 ...
随机推荐
- spring中实现自己的初始化逻辑
实现这两个listener都可以进行自己的初始化逻辑. InitializingBean.afterPropertiesSet 这个优先调用 ApplicationListener.onApplica ...
- 如何用Python输出一个斐波那契Fibonacci数列
a,b = 0, 1 while b<100: print (b), a, b = b, a+b
- python面向对象(二)
属性查找 类有两种属性:数据属性和函数属性 1. 类的数据属性是所有对象共享的 2. 类的函数属性是绑定给对象用的 class BeijingStudent: school='Beijing' ...
- 通过存储过程运行通过DBLINK的查询语句失败-单个语句成功--ORA-00604
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/q947817003/article/details/24419459 客户遇到个问题,描写叙述例如以 ...
- 016-Hadoop Hive sql语法详解6-job输入输出优化、数据剪裁、减少job数、动态分区
一.job输入输出优化 善用muti-insert.union all,不同表的union all相当于multiple inputs,同一个表的union all,相当map一次输出多条 示例 二. ...
- 爬虫四 selenium模块详细参数
selenium元素定位方法 一.访问页面并获取网页html from selenium import webdriver browser = webdriver.Chrome() browser.g ...
- 两张Excel表比较,两个for循环比较优化使用Contains
将一个Excel表中的sheet中的一列导出到List<string>,用一个for循环循环另一张表中的数据,看是否在第一张表中的Contains中
- deeplink
http://www.cnblogs.com/shadajin/p/5724117.html Deeplink,简单讲,就是你在手机上点击一个链接之后,可以直接链接到app内部的某个页面,而不是app ...
- python glob
http://python.jobbole.com/81552/ glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用 ...
- 【HackerRank】Service Lane
Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light o ...