2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965
题目:
iven two depth-first-search (DFS) sequences of a binary tree, can you find a binary tree which satisfies both of the DFS sequences?
Recall that a binary tree is a tree in which each vertex has at most two children, and the depth-first search is a tree traversing method which starts at the root and explores as far as possible along each branch before backtracking.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 105), indicating the number of vertices in the binary tree.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ∀ 1 ≤ i < j ≤ n, ai ≠ aj), indicating the first DFS sequence of the binary tree.
The third line of each test case contains n integers b1, b2, ..., bn (1 ≤ bi ≤ n, ∀ 1 ≤ i < j ≤ n, bi ≠ bj), indicating the second DFS sequence of the binary tree.
It is guaranteed that the sum of n over all test cases does not exceed 106, and there always exists at least one possible binary tree.
We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.
Output
For each test case, output one line which contains n integers seperated by one space. The i-th integer indicates the father of the i-th vertex in the binary tree which satisfies both of the DFS sequence. If the i-th vertex is the root of the binary tree, output 0 as its father. If there are multiple valid answers, you can output any of them.
Please, DO NOT print extra spaces at the end of each line, or your program may get a "wrong answer" verdict as this problem is special judged.
Sample Input
2
6
3 4 2 5 1 6
3 4 5 2 1 6
3
1 2 3
1 2 3
Sample Output
3 4 0 3 4 1
0 1 2 思路:

通过上图(第一个dfs序列记为a,第二个记为b)
可以看出对于一个没有兄弟节点的节点x,他必然是紧跟在x的父亲节点f的后面。
对于一个有兄弟节点的节点x(兄弟节点记为y),节点x或y必然紧跟在父亲节点f的后面。
所以可以得出结论:通过对比父亲节点后面第一个点是否相同,我们可以判断这父亲节点有几个儿子节点。
如果父亲节点f有两个子节点,但在ab数列中都是先访问左儿子,后访问右儿子,这时似乎结论错了,通过结论只能判断出只有一个子节点。
但这时可以把右儿子当左儿子的子孙节点的儿子节点,这时构造出的树依旧符合两个dfs序列。
这时怎么判断是知道了,但是怎么确定父亲节点呢?
对于在数列ab中f的下一个位置的数相同,则f有一个儿子,那么在数列ab中f的下一个位置就是下一个父亲节点。
如果在数列ab中f的下一个位置的数不同,则f有两个儿子x,y,把x,y当做下一个父亲节点进行递归就好了。
那怎么确定x和y在ab数列中的位置呢?哈希一下即可。
现在可以通过函数solve(int x,int y,int fa),进行递归判断fa后面的节点a[x],b[y]是否相同来确定fa的子节点个数。
void solve(int x,int y,int fa)
{
ans[a[x]]=ans[b[y]]=fa;
if(x>=n||y>=n)return ;
if(a[x]==b[y])
solve(x+,y+,a[x]);
else
{
solve(x+,hb[a[x]]+,a[x]);//hb是哈希x在b中位置
solve(ha[b[y]]+,y+,b[y]);//ha同理
}
}
但是如果有如下数列:
ab.....c......xxxxxx
ac.....b......xxxxxx
上面的代码会出现问题,其递归过程是

这时从第一数列递归访问完区间bc内的数后,本应该直接return,但代码却还是会继续访问
所以函数还需要加入一个参数,访问的最远距离,可以sz代表第一个数列中能访问到的最远位置。
并且当a[x]!=b[y]时还应递归访问xxxxx。
最终代码如下:
void solve(int x,int y,int fa,int sz)
{
if(x>sz)return;
ans[a[x]]=ans[b[y]]=fa;
if(a[x]==b[y]) sum[fa]+=;
else sum[fa]+=;
if(x==sz)return;
if(a[x]==b[y])
solve(x+,y+,a[x],sz);
else
{
int ta,tb;
ta=ha[b[y]]-,tb=hb[a[x]]-y+ta;
if(x!=ta)
solve(x+,hb[a[x]]+,a[x],ta);
if(y!=hb[a[x]]-)
solve(ta+,y+,b[y],tb);
if(tb!=sz)
solve(tb+,tb+-x+y,fd(ans[a[x]]),sz);
}
}
完整代码:
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
#define ll first
#define rr second
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,a[K],b[K],sum[K],ans[K];
int ha[K],hb[K]; int fd(int x)
{
return sum[x]==?fd(ans[x]):x;
}
void solve(int x,int y,int fa,int sz)
{
if(x>sz)return;
ans[a[x]]=ans[b[y]]=fa;
if(a[x]==b[y]) sum[fa]+=;
else sum[fa]+=;
if(x==sz)return;
if(a[x]==b[y])
solve(x+,y+,a[x],sz);
else
{
int ta,tb;
ta=ha[b[y]]-,tb=hb[a[x]]-y+ta;
if(x!=ta)
solve(x+,hb[a[x]]+,a[x],ta);
if(y!=hb[a[x]]-)
solve(ta+,y+,b[y],tb);
if(tb!=sz)
solve(tb+,tb+-x+y,fd(ans[a[x]]),sz);
}
}
int main(void)
{
std::ios::sync_with_stdio(false);
std::cin.tie();
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i],ha[a[i]]=i,sum[i]=;
for(int i=;i<=n;i++)
cin>>b[i],hb[b[i]]=i;
solve(,,,n);
for(int i=;i<n;i++)
cout<<ans[i]<<" ";
cout<<ans[n]<<endl;
}
return ;
}
2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965的更多相关文章
- 2017浙江省赛 E - Seven Segment Display ZOJ - 3962
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目: A seven segment display, or ...
- 2017浙江省赛 D - Let's Chat ZOJ - 3961
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题目: ACM (ACMers' Chatting Messe ...
- 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 ...
- 北邮校赛 H. Black-white Tree (猜的)
H. Black-white Tree 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...
- 2017浙江省赛 A - Cooking Competition ZOJ - 3958
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 题目: "Miss Kobayashi's Drag ...
- 2017浙江省赛 C - What Kind of Friends Are You? ZOJ - 3960
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3960 题目: Japari Park is a large zoo ...
- 2017浙江省赛 B - Problem Preparation ZOJ - 3959
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 题目: It's time to prepare the pr ...
- 2015 浙江省赛 H - May Day Holiday
H - May Day Holiday As a university advocating self-learning and work-rest balance, Marjar Universit ...
随机推荐
- int main(int argc, char *argv[])中的argc和argv
argc 是 argument count的缩写,表示传入main函数的参数个数: argv 是 argument vector的缩写,表示传入main函数的参数序列或指针,并且第一个参数argv[0 ...
- jquery获取设置input值
$("#input").val("123"),注意val()这个函数$("#input").val("123"),//给 ...
- 数据库布尔型状态字段互斥性的SQL更新操作
一个配置表或者一个存储了多条状态的表,需要在某个状态中做切换,而当前是否启用状态标记是用0和1来标识的.这个时候通常 1表示正在使用中,0表示不在使用中.通常有些业务会做一些配置的状态切换,就会出现要 ...
- iOS开发之--iOS APP打包的时候出现的四个选项
- 六 Android Studio打包Eegret App (解决开机黑屏问题)
因为android studio中的SplashActivity并没有什么卵用,只是开机1s显示开机画面,1s后面还是黑屏. 在主文件中加入以下代码,就是开始游戏时显示一个居中填满屏幕的图片,游戏加载 ...
- 《从零开始学Swift》学习笔记(Day 66)——Cocoa Touch设计模式及应用之通知机制
原创文章,欢迎转载.转载请注明:关东升的博客 通知(Notification)机制是基于观察者(Observer)模式也叫发布/订阅(Publish/Subscribe)模式,是 MVC( 模型-视图 ...
- Docker的基本使用(部署python项目)
今天开始利用docker来部署项目,当然,首先,需要安装好Docker,这个在我的上篇中写了 一.准备项目 我写的是一个爬取某ppt网站的代码,就一个ppt1.py是爬虫,然后,ppts是存放下载的p ...
- 设置 SSH Key 登录服务器和 Git 服务器
设置 SSH Key 登录服务器 通过 ssh 登录服务器,一直都是用的账号和密码,今天看到一篇文章说这样不安全,使用 ssh key 的方式登录则是更好的选择,因此,研究实践了一下,并记录在这里. ...
- 技术宅之flappy bird 二逼鸟
师雪坤和刘阳 风靡一时的虐心小游戏<Flappy Bird>,以玩法简单.难度超高著称,不过,最近这款让全世界玩家几欲怒摔手机的游戏,被两位中国技术宅设计的"玩鸟机器人" ...
- R语言中的MySQL操作
R语言中,针对MySQL数据库的操作执行其实也有很多中方式.本人觉得,熟练掌握一种便可,下面主要就个人的学习使用情况,总结其中一种情况-----使用RMySQL操作数据库. 1.下载DBI和RMySQ ...