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的更多相关文章

  1. PAT甲级1143 Lowest Common Ancestor【BST】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 2017战略No.1:坚定不移地走全产业链发展路线

    编者按:2016年9月9日,首次公开表达"我想走全产业链发展路线"的想法. 这几个月,认真思考了下这个决定背后的原因.目的和价值. 付出常人5倍以上的努力,先抓住"技术研 ...

  2. 「PKUSC2018」真实排名

    题面 题解 因为操作为将一些数字翻倍, 所以对于一个数\(x\), 能影响它的排名的的只有满足\(2y\geq x\)或\(2x>y\)的\(y\) 将选手的成绩排序,然后考虑当前点的方案 1. ...

  3. [HNOI2013]比赛 搜索

    [HNOI2013]比赛 搜索. LG传送门 直接暴力有60,考场上写的60,结果挂成40. 考虑在暴力的同时加个记忆化,把剩下的球队数和每支球队的得分情况hash一下,每次搜到还剩\(t\)个队的时 ...

  4. 洛咕 P3306 [SDOI2013]随机数生成器

    洛咕 P3306 [SDOI2013]随机数生成器 大力推式子??? \(X_{i}=\underbrace{a(a(\cdots(a(a}_{i-1个a}X_1+b)))\cdots)\) \(=b ...

  5. 洛谷P2973 [USACO10HOL]赶小猪

    https://www.luogu.org/problemnew/show/P2973 dp一遍,\(f_i=\sum_{edge(i,j)}\frac{f_j\times(1-\frac{P}{Q} ...

  6. bzoj 2238 Mst

    显然先求出最小生成树 如果删的是非树边就不用管,还是能取最小生成树 如果删的是树边就有非树边可以替代它 反向考虑,每条非树边可以替代最小生成树上一条路径的边 所以树剖加线段树,对每条非树边在树上更新对 ...

  7. [BZOJ3167][HEOI2013]SAO[树dp+组合数学]

    题意 给定 \(n\) 个节点和 \(n-1\) 个限制,每个节点有一个权值,每个限制形如:\(a_i< a_j\) ,问有多少个 \(1\) 到 \(n\) 排列满足要求. \(n\leq 1 ...

  8. Android Service(上)

    一 Service简介 Service是Context的子类 Service是四大组件之一 用来在后台处理一些比较耗时的操作或者去执行某些需要长期运行的任务 二 注意 Service里面不能直接执行耗 ...

  9. linux 开机报错,error grub_efi_find_mmap_size not find

    开机报错,差点以为要重装系统了 搜到了官方的重建引导的教程 修复了错误 https://wiki.manjaro.org/index.php/Restore_the_GRUB_Bootloader#F ...

  10. 零基础学python之构建web应用(入门级)

    构建一个web应用 前面的学习回顾: IDLE是Python内置的IDE,用来试验和执行Python代码,可以是单语句代码段,也可以是文本编辑器中的多语句程序. 四个内置数据结构:列表.字典.集合和元 ...