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 ...
随机推荐
- 杭电 2111 Saving HDU (贪心)
Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...
- 13-看图理解数据结构与算法系列(Trie树)
Trie树 Trie树,是一种搜索树,也称字典树或单词查找树,此外也称前缀树,因为某节点的后代存在共同的前缀.它的key都为字符串,能做到高效查询和插入,时间复杂度为O(k),k为字符串长度,缺点是如 ...
- 如何使用JDK1.6 API
如何使用JDK1.6 API-------https://jingyan.baidu.com/article/54b6b9c0e39a102d583b47d5.html
- Android BottomSheet:底部弹出Fragment面板(4)
Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出"重"的 ...
- DP 简单题目练习
ZOJ 1234 这道题目我表示也还不是特别能理解....还是太菜了T T 从后往前思考,因为只要后面有多的数在,那么C肯定是存在的,只要考虑是否把前两个数加在一起作为badness值这样两种情况来考 ...
- 选学霸(codevs 3372)
题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一部分没有,同学们就会抗议.所以老师想请你帮他求出他该选多少学霸,才能既不让同 ...
- 2.4 选择第k大的元素 selection
1.目标:找到N个元素中,第k大的数. 例如:max是k=N--1:min是k=0:median是k=N/2 2.Quick-select 借鉴了快速排序的思想 (1)利用partition保证: ① ...
- PatentTips - Register file supporting transactional processing
BACKGROUND OF THE INVENTION With the rise of multi-core, multi-threaded data processing systems, a k ...
- [bzoj3910]火车_并查集_倍增LCA
火车 bzoj-3910 题目大意:给定一棵n个节点的树,你需要顺次经过m个互不相同的节点,如果一个节点在之前的路径上被经过过,它不必再被特意经过.问走过的路径长度. 注释:$1\le n\le 5\ ...
- Writing Code-Codeforces511C**
http://codeforces.com/problemset/problem/544/C 完全背包 dp[i][j]表示第i行有j个bug #include<stdio.h> #inc ...