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 ...
随机推荐
- stl sort和qsort的使用
好不容易使用了下stl的qsort函数,顺便和sort函数一起总结下: 很多时候我们都需要用到排序. 例如: 1 #include <iostream> #include <algo ...
- Why Countries Succeed and Fail Economically
Countries Succeed and Fail Economically(第一部分)" title="Why Countries Succeed and Fail Econo ...
- 2017 计蒜之道 初赛 第一场 B阿里天池的新任务(简单)
题链:"https://nanti.jisuanke.com/t/15500" 本来希望通过找循环节然后套KMP来通过后面题的,可是只过了B题,可能循环节不一定是存在的. #inc ...
- [bzoj3668][Noi2014][起床困难综合症] (按位贪心)
Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找 ...
- Rim 边缘光
边缘光:计算眼睛和模型顶点法线的点积,结果作为强度,和材质输出:顶点和法线平行时,强度最大,垂直时,强度最小.因此将他取反,即同一方向时,强度最小,垂直时,强度最大. -dot(normalize(v ...
- Android BGABadgeView:BGABadgeFrameLayout(5)
Android BGABadgeView:BGABadgeFrameLayout(5) BGABadgeView除了有自己的线性布局,相对布局外(见附录文章7,8),还实现了FrameLayou ...
- POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8130 Accepted: 3 ...
- android listVIew实现button按钮监听程序
1.重写simpleAdapter 方法@Override public HashMap<String,String> getItem(int position) { // TODO Au ...
- Codeforces679C. Bear and Square Grid
n<=500,n*n的01矩阵,可以选择一个k*k的矩阵全变1,求最大1联通区域. 敢敢n^3..模拟k*k的矩阵的位置,从左到右扫的时候,每变一个位置只会引起边界的信息变化,就记含边界的k*k ...
- 【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link ...