ZOJ3965 Binary Tree Restoring
ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构。
考察树的递归性质,不要想的太复杂。
当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个儿子。如果子树中还有未连根的点则接到当前点下。son数组表示每个点的子树有多少个点。pos数组记录每个数在每个序列中的位置。dfs中p1,p2指向同一个数
lim1,lim2表示当前点子树可能最大的子树范围。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector> using namespace std;
#define maxn 110000
int n,fa[maxn];
int s1[maxn],s2[maxn];
int pos1[maxn],pos2[maxn];
int vis[maxn],son[maxn];
void dfs(int p1,int p2,int lim1,int lim2)
{
//printf("%d %d %d %d\n:::::\n",p1,lim1,p2,lim2);
//system("pause");
int now=s1[p1];
if(vis[now]) return;
vis[now]=1;
if(p1<=0||p1>n) return ;
if(p2<=0||p2>n) return ;
if(lim1<=0) return ;
if(lim2<=0) return ;
if(p1>lim1||p2>lim2) return;
son[now]=1;
if(lim1==p1||lim2==p2) return;
int r1=lim1,r2=lim2;
if(s1[p1+1]!=s2[p2+1]&&p1+1<=lim1&&p2+1<=lim2)
{
int len=0;
if(!fa[s1[p1+1]])
{
fa[s1[p1+1]]=now;
r1=pos1[s2[p2+1]]-1;
len=r1-p1;
dfs(p1+1, pos2[ s1[p1+1] ] , r1 ,pos2[ s1[p1+1] ]+len-1 );
son[now]+=son[ s1 [p1+1] ];
}
if(!fa[s2[p2+1]])
{
fa[s2[p2+1]]=now;
r2=pos2[s1[p1+1]]-1;
len=r2-p2;
dfs( pos1[s2[p2+1]] , p2+1, pos1[s2[p2+1]]+len-1, r2 );
son[now]+=son[ s2 [p2+1] ];
}
}
else if(s1[p1+1]==s2[p2+1]&&p1+1<=lim1&&p2+1<=lim2)
{
fa[ s1[p1+1] ] = now;
dfs(p1+1,p2+1,lim1,lim2);
son[now]+=son[ s1[p1+1] ];
int nt=p1+son[s1[p1+1]]+1;
if(son[now]<lim1-p1+1)
{
fa[s1[nt]]=now;
dfs(nt,pos2[s1[nt]],lim1,lim2);
son[now]+=son[s1[nt]];
}
}
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
memset(fa,0,sizeof(fa));
memset(vis,0,sizeof(vis));
memset(son,0,sizeof(son));
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
memset(pos1,0,sizeof(pos1));
memset(pos2,0,sizeof(pos2));
for(int i=1;i<=n;i++) scanf("%d",&s1[i]),pos1[s1[i]]=i;
for(int i=1;i<=n;i++) scanf("%d",&s2[i]),pos2[s2[i]]=i;
dfs(1,1,n,n);
for(int i=1;i<n;i++) printf("%d ",fa[i]);
printf("%d\n",fa[n]);
}
return 0;
}
ZOJ3965 Binary Tree Restoring的更多相关文章
- ZOJ 3965 Binary Tree Restoring
Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...
- zoj 3965 Binary Tree Restoring(搜索)
Binary Tree Restoring Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Given two ...
- 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [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, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- Linux最常用的基础命令 下篇
Linux最常用的基础命令个人总结 shell脚本 脚本就是:写一堆指令存成一个文本,用于完成一些小任务 a="123" linux中定义一个变量 echo $a echo $b ...
- python之更加抽象 2014-4-6
#更加抽象 12:50pm- 14:50 p112- 1.对象的魔力 多态 如count 在多种数据类型中都可以实现计数的功能 封装 对全局作用域中其他区域隐藏多余信息的原则 继承2.类和类型 创建类 ...
- C51 使用端口 个人笔记
使用整个端口的8个引脚: 八个引脚,需要8位2进制,2位十六进制 #define P0 led led = 0x3f; //led = ~0x3f; 使用某个端口的某一个引脚 sbit led = P ...
- javaWeb学习之 Filter过滤器----https://www.cnblogs.com/xdp-gacl/p/3948353.html
https://www.cnblogs.com/xdp-gacl/p/3948353.html
- 网络编程基础:网络基础之网络协议、socket模块
操作系统(简称OS)基础: 应用软件不能直接操作硬件,能直接操作硬件的只有操作系统:所以,应用软件可以通过操作系统来间接操作硬件 网络基础之网络协议: 网络通讯原理: 连接两台计算机之间的Intern ...
- 【intellij】intellij idea 建立与src级别的目录
在使用三大框架时,通常会把配置文件放在自己新建的config文件夹里,以便编程.在 myeclipse里新建的config文件夹是Source Folder属性的 这样他的级别适合src一个级别,但是 ...
- [bzoj3712][PA2014]Fiolki_倍增LCA
Fiolki bzoj-3712 PA-2014 题目大意:题目链接. 注释:略. 想法: 神题! 我们建树:对于一次倾倒操作,我们弄一个新的大瓶子作为两个合并瓶子的父亲节点,与两个瓶子相连. 对于一 ...
- 【SQL Server 学习系列】-- 收缩数据库文件大小
USE WebExam; GO ALTER DATABASE WebExam SET RECOVERY SIMPLE; GO -- 收缩文件到 1 MB. ); GO ALTER DATABASE W ...
- Swift可选类型(Optional)之星耀
首先我们先看下Objective-C与Swift语言对于可选nil的不同理解: Objective-C中的nil:表示缺少一个合法的对象,是指向不存在对象的指针,对结构体.枚举等类型不起作用(会返回N ...
- Zookeeper学习 & Paxos
首先,Zookeeper是基于Paxos来进行分布式选举管理的,Paxos的内容可以参考我另一篇文章:http://www.cnblogs.com/charlesblc/p/6037004.html ...