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

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

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100000 + 10;
 4 int a[N], b[N];         /// 序列 A, B
 5 int posa[N], posb[N];   /// 节点值 i 在序列 A(B) 中的位置  哈希表
 6 int par[N];              /// 记录节点值 i 的父节点值
 7 int pnt;                /// pnt-1 表示当前已知道了多少个节点的父节点 -> 即 pnt 为之后需处理第 pnt 个点的父节点
 8 void solve(int pos,int l,int r,int fa){ ///当前需处理 A[pos] 的父节点,对应 B 序列的区间为 [l,r]
 9     if(l>r) return;
10     if(a[pos]==b[l]){///如果当前要确定的节点和第二个串第一个字母是一样的
11         par[a[pos]]=fa;
12         pnt++;
13         solve(pos+1,l+1,r,a[pos]);
14         if(pnt-pos<=r-l){
15             int tmp=pnt;
16             par[a[tmp]]=fa;
17             pnt++;
18             solve(tmp+1,l+tmp-pos+1,r,a[tmp]);
19         }
20     }
21     else{
22         par[a[pos]]=fa;
23         pnt++;
24         int len1=posa[b[l]]-pos-1;
25         solve(pos+1,posb[a[pos]]+1,posb[a[pos]]+len1,a[pos]);
26         par[b[l]]=fa;
27         pnt++;
28         solve(posa[b[l]]+1,l+1,posb[a[pos]]-1,b[l]);
29     }
30 }
31 int main()
32 {
33     int T,n;
34     scanf("%d",&T);
35     while(T--)
36     {
37         scanf("%d",&n);
38         for(int i=1;i<=n;++i)
39             scanf("%d",&a[i]),posa[a[i]]=i;
40 
41         for(int i=1;i<=n;++i)
42             scanf("%d",&b[i]),posb[b[i]]=i;
43 
44         pnt=1;
45         solve(1,1,n,0);
46         for(int i=1;i<=n;++i){
47             if(i>1) printf(" ");
48             printf("%d",par[i]);
49         }
50         printf("
");
51     }
52 }
原文地址:https://www.cnblogs.com/gongpixin/p/6767168.html