ZOJ 3965 构造

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


题意:给你两个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;
} 
/*
100
9
1 2 3 4 5 6 7 8 9
1 2 3 5 4 6 8 7 9
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值