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 ...
随机推荐
- 贪心 Radar Installation (求最少探测雷达)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- Spring核心技术(七)——Spring容器的扩展
本文将讨论如何关于在Spring生命周期中扩展Spring中的Bean功能. 容器的扩展 通常来说,开发者不需要通过继承ApplicationContext来实现自己的子类扩展功能.但是Spring ...
- 圆角计算 Shader
圆角的计算 在Shader中,我们使用UV坐标来计算需要显示的部分和不需要显示的部分,使用透明来处理显示与不显示.UV坐标如下图1,我们将坐标平移到图2位置,面片的UV坐标原点在面片中心,UV坐标范围 ...
- SQL Server 2016 CTP3.2 开荒 Reporting Service 篇
仅仅是开荒资源页,反正过不了多久就会有新的CTP. 下面是MSDN I Tell you 提供的 不过是中文,个人不是很建议,因为现在大多的资源页都是英文的ed2k://|file|cn_sql_se ...
- 【ZJOI2017 Round1练习】D8T2 sequence(DP)
题意: 思路: #include <algorithm> #include <iostream> #include <cstring> #include <c ...
- POJ 3255_Roadblocks
题意: 无向图,求单源次短路,每条边可以走多次. 分析: 对于每个点记录最短路和次短路,次短路可以是由最短路+边或者是次短路+边更新而来.在更新每个点的最短路时,相应更新次短路,如果最短路更新了,就令 ...
- Ubuntu 16.04修改显示字体大小(包括GNOME/Unity)
在Ubuntu中字体都基本偏大,且和分辨率无关. Unity: 安装Unity Tweak Tool sudo apt-get install unity-tweak-tool GNOME: 打开优化 ...
- java文件工具类
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- openFileOutput 文件属性设置、主动配置文件的可读写属性及事实上现方式
首先參考 Android 内部存储相关的函数(getCacheDir,getDir, getFileStreamPath,getFilesDir,openFileInput, ...) 1. 用ope ...
- vue 重要的东西