Binary Tree Restoring

思路:

递归

比较a序列和b序列中表示同一个子树的一段区间,不断递归

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e5+;
int a[N],b[N],par[N],posa[N],posb[N],sum[N],n;
void dfs(int l,int r,int ll,int rr,int pa){
if(l>r||ll>rr||r>n||rr>n)return ;
if(a[l]==b[ll]){
par[a[l]]=pa;
sum[pa]++;
dfs(l+,r,ll+,rr,a[l]);
}
else{
par[a[l]]=par[b[ll]]=pa;
sum[pa]+=;
int len1=posa[b[ll]]-l;
int len2=posb[a[l]]-ll;
dfs(l+,l+len1-,posb[a[l]]+,posb[a[l]]+len1-,a[l]);
dfs(posa[b[ll]]+,posa[b[ll]]+len2-,ll+,ll+len2-,b[ll]);
while(sum[pa]==)pa=par[pa];
dfs(l+len1+len2,r,ll+len1+len2,rr,pa);
}
}
int main(){
int T;
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;
mem(sum,);
dfs(,n,,n,);
for(int i=;i<=n;i++)printf("%d%c",par[i]," \n"[i==n]);
}
return ;
}

ZOJ 3965 Binary Tree Restoring的更多相关文章

  1. zoj 3965 Binary Tree Restoring(搜索)

    Binary Tree Restoring Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge Given two ...

  2. 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...

  3. ZOJ3965 Binary Tree Restoring

    ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...

  4. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  7. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  9. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

随机推荐

  1. DBeaver连接Oracle11g数据库

    DBeaver连接Oracle11g数据库 一.准备 (1)dbeaver管理软件 (2)远程连接数据库地址.用户名秘密等 (3)Oracle驱动:ojdbc6.jar工具包 下载地址:https:/ ...

  2. stm32最简单的实现BootLoader

    BootLoader大家应该都知道是干什么的,简单的来说就是程序开始运行前的一段程序. 在成熟的产品中,通常都是采用BootLoader方式来升级产品的程序.也就是IAP升级.在了解完基本的实现原理后 ...

  3. Git本地分支和远程分支关联

    转载:https://blog.csdn.net/cherishhere/article/details/52606884 转载:https://blog.zengrong.net/post/1746 ...

  4. Junit 的Assertions的使用

    import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import ...

  5. 王之泰201771010131《面向对象程序设计(java)》第一周学习总结

    王之泰201771010131<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.co ...

  6. Git rebase的使用

    rebase 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. 在本节中我们将学习什么是“rebase”,怎样使用“rebase”,并将展示该操作的惊艳之处,以及指 ...

  7. Android Java层,Native层,Lib层打印Log简介【转】

    本文转载自:https://blog.csdn.net/AndroidMage/article/details/52225068 说明: 这里我根据个人工作情况说明在各个层打印log.如有问题欢迎拍砖 ...

  8. hihoCoder week8 状态压缩·一

    状态压缩  写了两个半小时  太菜了 题目链接 https://hihocoder.com/contest/hiho8/problem/1 #include <bits/stdc++.h> ...

  9. html 之 table cellpadding,cellspacing

    单元格(cell) -- 表格的内容 单元格边距(表格填充)(cellpadding) -- 代表单元格外面的一个距离,用于隔开单元格与单元格空间 单元格间距(表格间距)(cellspacing) - ...

  10. LuoguP2257 YY的GCD

    题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 kAc这种傻×必然不会了,于 ...