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 ...
随机推荐
- PyQt5用QTimer编写电子时钟
[说明] 本文用 PyQt5 的QTimer类的两种方式实现电子时钟 [效果图] [知识点] QTimer类提供了定时器信号/槽和单触发定时器. 它在内部使用定时器事件来提供更通用的定时器. QTim ...
- 【HNOI2014】江南乐
题面 题解 知识引入 - \(SG\)函数 任何一个公平组合游戏都可以通过把每个局面看成一个顶点,对每个局面和它的子局面连一条有向边来抽象成这个"有向图游戏".下面我们就在有向无环 ...
- Spring Boot:项目打包成war并发布到Tomcat上运行
一.修改pom文件 1. 因为SpringBoot中嵌入的有Tomcat,因此要移除嵌入式的Tomcat插件 <dependency> <groupId>org.springf ...
- 【无图慎入】Link Cut Tree 总结
link-cut tree 动态树(准确说是维护森林)之一,支持连边,断边,求链上权值和等操作. splay基础:会rotate和splay就行.还要会一点区间反转操作打标记.很基♂础的东西. 有重链 ...
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...
- Linux 和 Windows 之间共享文件之 samba
导语 如果对windows有过实际操作技巧的人都会明白,在windows下的文件共享加上网络驱动器映射是多么方便的体验,甚至比ftp更加的简单,就像本地多了一块可与他人交流的硬盘一样. 问题 由于性能 ...
- Maven 依赖节点总结
首先是log4j: <!--哎,神特么的log4j,版本不对就Spring AOP前置增强 new不出来--><dependency> <groupId>log4j ...
- Magic Trackpad 2 on win10 x64
可以使用操作: 触击 触击拖拽 双击 支手滚动:上下,左右 右键配置:左下角,右下角点击实现 可以试用28天,收费 $9.5 官方地址:http://www.trackpadmagic.com/mag ...
- jenkins 自动上传代码到nexus 私库
1.jenkins 项目配置上传 2.jenkins 访问私库下载配置 -X clean install 3.maven 配置文件 /usr/local/maven/conf/settings.xml ...
- java学习(二)基础概念、语法
对象 类的实例(通俗点讲,new出来的玩意好像都是对象?初学者的感觉,不造对错啊,有大神给我解释下可以啊) 类 class嘛,模板嘛,可以给对象实例的嘛 方法 行为,学编程的,方法,这玩意心里都懂吧, ...