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. MD5与SHA散列单项加密

    MD5 MD5的英文全称是Message Digest Algorithm MD5,译为消息摘要算法第五版,是众多哈希算法中的一种(哈希算法是一种可以将任意长度的输入转化为固定长度输出的算法).因此M ...

  2. Python3 自定义请求头消息headers

    Python3 自定义请求头消息headers 使用python爬虫爬取数据的时候,经常会遇到一些网站的反爬虫措施,一般就是针对于headers中的User-Agent,如果没有对headers进行设 ...

  3. maven单元测试报java.lang.IllegalStateException: Failed to load ApplicationContext

    报这个异常java.lang.IllegalStateException: Failed to load ApplicationContext的时候,通常是因为applicationContent.x ...

  4. (5keras自带的模型之间的关系)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

    ​ ​其中: 1.VGG 网络以及从 2012 年以来的 AlexNet 都遵循现在的基本卷积网络的原型布局:一系列卷积层.最大池化层和激活层,最后还有一些全连接的分类层. 2.ResNet 的作者将 ...

  5. python --- 25 模块和包

    一.模块 1.导入方式 自己创建的模块名称 切不可和 内置模块的一样 ①  import  模块 ②  import 模块 as  名      设置在此空间的名称 ③  from 模块 import ...

  6. uniGUI试用笔记(四)

    uniGUI下有专用的登录窗体类:TUniLoginForm,该类属于AppForm,构建代码为: function frmWebLogin: TfrmWebLogin; begin Result : ...

  7. centos6.8下如何升级git版本?

    1. 安装开发环境 yum install -y curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...

  8. vi中如何使用cscope来查找函数的定义

    答:进入命令行模式输入:cs f g <function_name>

  9. rocketmq总结(消息的顺序、重复、事务、消费模式)

    rocketmq总结(消息的顺序.重复.事务.消费模式) 参考: http://www.cnblogs.com/wxd0108/p/6038543.html https://www.cnblogs.c ...

  10. Windows FindFirstFile利用

    目前大多数程序都会对上传的文件名加入时间戳等字符再进行MD5,然后下载文件的时候通过保存在数据库里的文件ID读取文件路径,一样也实现了文件下载,这样我们就无法直接得到我们上传的webshell文件路径 ...