ZOJ 3965 构造

来源:互联网 发布:微信会员卡源码下载 编辑:程序博客网 时间:2024/06/10 19:11

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

263 4 2 5 1 63 4 5 2 1 631 2 31 2 3

Sample Output

3 4 0 3 4 10 1 2


题意:给你两个dfs序  构造一个二叉树满足这两个dfs序  输出每个结点的父亲  如果是根就输出0


题解:

dfs搜下去

如果当前结点相同  就继续匹配下去

如果当前结点不同  找到两个序列中对应的位置继续匹配

如果一个结点已经有2个儿子但是还是有剩余的没有匹配完  就把剩余的扔给父亲


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[100005],b[100005],visa[100005],visb[100005],ans[100005],zi;int solve(int l1,int r1,int l2,int r2,int pre){if(r1<l1||r2<l2)return 0;if(a[l1]==b[l2]){ans[a[l1]]=pre;int re=solve(l1+1,r1,l2+1,r2,a[l1]);if(l1+re<r1&&zi!=2){zi=2;return 1+re+solve(l1+re+1,r1,l2+re+1,r2,a[l1]);}zi=1;return 1+re;}else{int posa=visa[b[l2]];int len=posa-l1;int posb=visb[a[l1]];int lens=posb-l2;solve(l1,l1+len-1,posb,posb+len-1,pre);solve(posa,posa+lens-1,l2,l2+lens-1,pre);zi=2;return len+lens;}}int main(){int t;scanf("%d",&t);while(t--){int n,i,j;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i]);visa[a[i]]=i;}for(i=1;i<=n;i++){scanf("%d",&b[i]);visb[b[i]]=i;}solve(1,n,1,n,0);for(i=1;i<=n;i++){printf("%d",ans[i]);if(i==n)printf("\n");else printf(" ");}}return 0;} /*10091 2 3 4 5 6 7 8 91 2 3 5 4 6 8 7 9*/


0 0
原创粉丝点击