PAT 甲级 1143 Lowest Common Ancestor
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
A binary search tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
Given any two nodes in a BST, you are supposed to find their LCA.
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 BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. 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 BST, 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:
6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
代码:
#include <bits/stdc++.h>
using namespace std; int N, M;
vector<int> pre;
map<int, int> mp; int main() {
scanf("%d%d", &M, &N);
pre.resize(N);
for(int i = 0; i < N; i ++) {
scanf("%d", &pre[i]);
mp[pre[i]] = 1;
}
while(M --) {
int a, b;
scanf("%d%d", &a, &b);
if(!mp[a] && !mp[b])
printf("ERROR: %d and %d are not found.\n", a, b);
else if(!mp[a] || !mp[b])
printf("ERROR: %d is not found.\n", mp[a] ? b : a);
else {
int root;
for(int i = 0; i < N; i ++) {
root = pre[i];
if((root >= a && root <= b) || (root <= a && root >= b)) break;
} if(root == a)
printf("%d is an ancestor of %d.\n", a, b);
else if(root == b)
printf("%d is an ancestor of %d.\n", b, a);
else printf("LCA of %d and %d is %d.\n", a, b, root);
}
}
return 0;
}
用 mp 记下是否出现过 然后只要从头找满足值在 a b 之间的就是根了
FHFHFH 过年之前最后一个工作日
PAT 甲级 1143 Lowest Common Ancestor的更多相关文章
- PAT甲级1143 Lowest Common Ancestor【BST】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...
- PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- PAT 1143 Lowest Common Ancestor[难][BST性质]
1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- [PAT] 1143 Lowest Common Ancestor(30 分)
1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- PAT 1143 Lowest Common Ancestor
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 1143 Lowest Common Ancestor
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 1143. Lowest Common Ancestor (30)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
随机推荐
- BZOJ1190_梦幻岛宝珠_KEY
题目传送门 观察数据a*2^b,转化成二进制后,后面跟了b位的0,可以转化为一个分层背包. 先预处理出每个物品是哪一层的,并放在同层内DP. 同层内直接背包,考虑层与层之间的DP. 第一维枚举层数,然 ...
- 如何补装oracle的sample schema
SQL>@ORACLE_HOME/rdbms/admin/utlsampl.sql; 似乎不够完整,等待补充.
- kali base64命令的使用
1.启动终端 2.输入base64 3.输入需要编码的字符串 4,CTRL+D,结束输入,在屏幕上回显输出结果
- 洛咕 P2494 [SDOI2011]保密
出题人没素质啊,强行拼题还把题面写得又臭又长. 简单题面就是有一张图,每条边有两个权值\(t,s\),有无限支军队,一支军队可以打一个点,代价是从n到这个点的路径的\(\frac{\sum t}{\s ...
- JZOJ 10043 第k小数
Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到NM个数,询问这NM个数中第K小数是多少. 时间限制为20ms . Input 输入文 ...
- IDEA 出现 updating indices 卡进度条问题的解决方案并加快索引速度
缺点: 这样的话,前端的接口(也就是字符串)就搜索不到了. C:\Users\Administrator\.IntelliJIdea2017.3\system 删除里面的caches文件夹(这里的 ...
- Object C学习笔记7-字符串NSString之一
在Object C中存在两个类用于操作字符串,NSString和NSMutableString;NSString在赋值之后不能修改其内容和长度,而NSMutableString可以动态的修改字符串内容 ...
- 解决table td里面长串数字或字母不换行的问题
在html中,经常要用到table标签,一般情况下,table下面的td元素里的东西都是汉字或者说是汉字.字母.数字的混合,在这种情况下,不设置table的宽度,也就是table宽度自适应的时候,浏览 ...
- 用Micro:bit播放生日快乐歌
Micro:bit自带一个有趣的功能就是可以生成音乐播放,今天做一个简单实用的案例,用Micro:bit播放生日快乐歌. 算法: 按下按键A,显示生日快乐 播放D 播放D 播放E 播放D 播放G 播放 ...
- Discuz x3.2利用阿里云cdn处理https访问亲测教程
第一步配置cdn和https 1.首先去阿里云.腾讯云.七牛云等申请免费https证书 2.虚拟主机是不能直接支持https的,需要cdn处理后才可以,并且端口是80 3.开启cdn加速处理,(买一个 ...