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. django模板-通过a标签生成链接并跳转

    views.py from django.shortcuts import render from django.http import HttpResponse def index(request) ...

  2. Linux LAMP架构搭建

    一.部署LAMP基本架构 1.L(linux)A(apache)M(mysql)P(php) 2.稳定版本(mysql5.1,php5.3,apache2.2) 3.安装顺序,mysql-->a ...

  3. sql xml 入门 (二)

    DECLARE @myDoc xml --http://www.paymob.cn --话费充值api,充值api,话费充值接口,手机话费充值,车贝手机,贝萌手机,移动话费充值,联通话费充值,电信话费 ...

  4. 一次 Java 内存泄漏排查过程,涨姿势

    人人都会犯错,但一些错误是如此的荒谬,我想不通怎么会有人犯这种错误.更没想到的是,这种事竟发生在了我们身上.当然,这种东西只有事后才能发现真相.接下来,我将讲述一系列最近在我们一个应用上犯过的这种错误 ...

  5. 论证与测试 + 用EA画uml

    论证与测试,谁才是真正的不二法门 第十三次作业的时候,我们开始使用Junit对代码进行测试,主要是测试代码的覆盖率,以及分支的覆盖率.(主要是检查JSF写的是否是符合规范,……). 这里我给出我测试的 ...

  6. CAN通信工作原理个人心得

    CAN总线结构示意图: 说明: 1:CAN收发器(示意图中的单元)根据两总线CAN_H和CAN_L的电位差来判断总线电平: 2:实际中CAN_H与CAN_L由双绞线组成: 3:数据传递终端的电阻器,是 ...

  7. java利用poi解析excel文件

    首先需要引入以下jar包 如果使用maven,需要添加两个依赖 <dependencies> <dependency> <groupId>org.apache.po ...

  8. log4j2使用介绍

    工作中,用到了log4j2,以前只接触过log4j,也没有太过深入,这次就稍微系统的学习了以下log4j2. 一.引入pom.xml 使用maven作为项目的构建环境,pom.xml使用slf4j,s ...

  9. MongoDB的常用操作总结

    简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. ...

  10. Ubuntu18.04 安装搜狗输入法后无法启动的问题

    ibus 改选成fcitx后搜狗输入法照样没出来. 这里需要im-config 没有的话需要安装: sudo apt install im-config 然后在Terminal中执行 im-confi ...