A1151 LCA in a Binary Tree (30 point(s))

寻找两结点的共同祖先

1. 原文

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int

output Specification:

For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y. where X is A and Y is the other node. If U or V is not found in the binary tree, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

Sample Input:

1
2
3
4
5
6
7
8
9
6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99

Sample output:

1
2
3
4
5
6
LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

2. 解析

记录出现过的值 –> map<int,int> exist; 使用数组会超时,N个结点的值并不是1-N

树的中序inorder记录了结点的树结构 左 根 右 –> 记录值出现顺序 exist[in[i]]=i;

树的先序preorder最先出现的必是结点根root

中序+先序=树

查找u, v,root的位置:

  • u<root并且v<root 则u,v的根在当前根的左子树
  • u>root并且v<root 或u<root并且v>root,两者刚好在根的两侧,当前根即为Ancestor
  • u>root并且v>root 则u,v的根在当前根的右子树

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<cstdio>
#include<map>
using namespace std;
const int maxn=10010;
int in[maxn]={};
int pre[maxn]={};
map<int,int> exist;
void LCA(int l,int r,int root,int u,int v)
{
if (l>r)
{
return;
}
int rootIdx=exist[pre[root]];
int uIdx=exist[u];
int vIdx=exist[v];
if (uIdx<rootIdx&&vIdx<rootIdx)
{
LCA(l,rootIdx-1,root+1,u,v);
}else if ((uIdx>rootIdx&&vIdx<rootIdx)||(uIdx<rootIdx&&vIdx>rootIdx))
{
printf("LCA of %d and %d is %d.\n",u,v,pre[root]);
}else if(uIdx>rootIdx&&vIdx>rootIdx){
LCA(rootIdx+1,r,root+1+rootIdx-l,u,v);
}else if (uIdx==rootIdx)
{
printf("%d is an ancestor of %d.\n",u,v);
}else if (vIdx==rootIdx){
printf("%d is an ancestor of %d.\n",v,u);
}

}
int main()
{
int m,n;
scanf("%d%d",&m,&n);
for (int i = 1; i <= n; ++i)
{
scanf("%d",&in[i]);
exist[in[i]]=i;
}
for (int i = 1; i <= n; ++i)
{
scanf("%d",&pre[i]);
}
int u,v;
while(m--){
scanf("%d%d",&u,&v);
if (exist[u]==0&&exist[v]==0)
{
printf("ERROR: %d and %d are not found.\n",u,v);
}else if(exist[u]==0){
printf("ERROR: %d is not found.\n",u);
}else if (exist[v]==0)
{
printf("ERROR: %d is not found.\n",v);
}else{
LCA(1,n,1,u,v);
}
}
return 0;
}

exist设为map,结点的值并不在[0,10000]内

本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1151/
  • 发布时间: 2019-09-02 10:16
  • 更新时间: 2021-10-29 14:11
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!