1143. Lowest Common Ancestor


1143. Lowest Common Ancestor (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), 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.


题意就是按照要求找到BST树中U和V点的最近公共祖先 并按照要求输出出来

我们可以直接遍历整个表 由于BST的先序序列 就是元素的插入顺序 也就是元素的深度优先搜索的顺序 我们可以直接扫描整个表 就相当于深度搜索整棵树 如果当前点符合输出条件就停下输出

但是后来一直错不知道为什么 最后参考了网上的代码才发现 原来即使找到了两个点的LCA 也要按照输入顺序 先输出U 再输出V

哎。。。这一个错找了好久好久 PAT果然对输入输出一点也不友好。。。

#include<bits/stdc++.h>
using namespace std;
int a[10010];
map<int,bool>Map;
int main()
{int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){ scanf("%d",&a[i]);Map[a[i]]=1;}while(n--){int s,e;scanf("%d%d",&s,&e);bool fs=0,fe=0;fs = Map[s],fe = Map[e];if(fs==0&&fe==0)printf("ERROR: %d and %d are not found.\n",s,e);		       else if(fs!=0&&fe==0)printf("ERROR: %d is not found.\n",e); else if(fs==0&&fe!=0)printf("ERROR: %d is not found.\n",s);        else{int ss = s,ee=e;s = min(ss,ee),e = max(ee,ss); for(int now = 1;now<=m;now++){if((s<a[now]&&e>a[now])){printf("LCA of %d and %d is %d.\n",ss,ee,a[now]);//就是这里输出如果是s,e 修改后的元素的输出 就会导致两个测试点出错break;}if(s==a[now]){printf("%d is an ancestor of %d.\n",s,e);break;}if(e==a[now]){printf("%d is an ancestor of %d.\n",e,s);break;}				} }}return 0;
}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/425239.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MYSQL 在当前时间加上或减去一个时间段

update user set time1now(),time2date_add(NOW(), interval 1 MONTH) where id1;date_add() 增加date_sub()减少month 月份minute 分钟second 秒 转载于:https://www.cnblogs.com/zsg88/p/10625638.html

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第6篇]我们怎么把NP问题解释成一组可以在多项式内证明的命题

这是密码学52件事的第六篇&#xff0c;我们继续解释复杂性理论&#xff0c;这篇我们给NP问题另一个定义。(注:就是说这节中&#xff0c;我们把**"问题是否为NP的"转换为另一组可以在多项式时间内判定的定理)**。 这个问题是紧接着上一周的问题的。上周我们回答了什么…

ghjk

客户看看基本转载于:https://www.cnblogs.com/Majintao/p/10628174.html

2018蓝桥杯模拟赛·青出于蓝而胜于蓝 DFS序+树状数组

武当派一共有 nnn 人&#xff0c;门派内 nnn 人按照武功高低进行排名&#xff0c;武功最高的人排名第 111&#xff0c;次高的人排名第 222&#xff0c;... 武功最低的人排名第 nnn。现在我们用武功的排名来给每个人标号&#xff0c;除了祖师爷&#xff0c;每个人都有一个师父&a…

[Leetcode][第785题][JAVA][判断二分图][BFS][DFS]

【问题描述】[中等] 【解答思路】 1. DFS 深度优先遍历 时间复杂度&#xff1a;O(NM) 空间复杂度&#xff1a;O(N) class Solution {private static final int UNCOLORED 0;private static final int RED 1;private static final int GREEN 2;private int[] color;privat…

[剑指offer]面试题第[68-2]题[Leetcode][第236题][JAVA][二叉搜索树的最近公共祖先][递归]

【问题描述】[中等] 235/68-1 搜索二叉树 236/68-2 二叉树 【解答思路】 递归 时间复杂度&#xff1a;O(N) 空间复杂度&#xff1a;O(N) 情况 1. , 2. , 3. , 4. 的展开写法如下。 class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, Tr…

Docker 查看镜像信息

Docker 查看镜像信息 原文:Docker 查看镜像信息文章首发个人网站&#xff1a; https://www.exception.site/docker/docker-look-up-image-info 本文中&#xff0c;我们将需要学习 Docker 如何查看镜像信息&#xff1f; 一、images 命令列出镜像 通过使用如下两个命令&#xff0…

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第7篇]随机性如何辅助计算和什么是BPP类问题

这篇是密码学52件事中第7篇.我们只要把问题集中在BPP复杂类问题. 目前为止,我们已经介绍了一些复杂类: P 是一类能在多项式时间内被可确定的图灵机判定的问题.NP是一类能在多项式时间内被非确定的图灵机判定的问题.BPP是一类在多项式时间内被概率图灵机解出的问题,并且对所有…

73 forward动作

定义一个logind的jsp页面 <% page language"java" import"java.util.*" contentType"text/html; charsetutf-8"%><!DOCTYPE html> <html> <head> <meta charset"ISO-8859-1"> <title>Insert tit…

Linux系统开发之路 - 下

5、Ubuntu安装好之后&#xff0c;就可以进行开发环境的搭建。&#xff08;坚持看完有彩蛋&#xff0c;(>--..--<).jpg&#xff09;。 1&#xff09;首先安装Nodejs和Npm。 打开浏览器输入nodejs.org&#xff0c;进入页面会提示下载&#xff0c;如下图&#xff0c;选择LTS…

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第8篇]交互式的定义如何帮助计算和IP类问题是什么

这是系列中的第8篇&#xff0c;我们主要讨论计算中交互作用的用处和IP类问题是什么. 什么是证明 经典的证明 交互式证明系统 [1] http://dl.acm.org/citation.cfm?id63434 [2] http://www.amazon.co.uk/Introduction-Theory-Computation-Michael-Sipser/dp/0619217642 [3] h…

线段树之扫描线思路

运用线段树扫描线方式可以解决经典的求矩形面积交问题以HDU_1542 Atlantis 题为例 线段树和扫描线是这么结合的线段树统计的是有效区间段的长度 也就是扫描线 当前扫描到的区间段是哪一个 什么意思 比如当前在哪一个段扫描 那么线段树中的t[1]中的len就是多长 线段树一般…

74 param动作

定义一个logind的jsp <% page language"java" import"java.util.*" contentType"text/html; charsetutf-8"%><!DOCTYPE html> <html> <head> <meta charset"ISO-8859-1"> <title>Insert title h…

Unknown column 'user_uid' in 'field list' sql错误解决过程

在idea中运行一直有错&#xff0c;找了好多个地方都找不到&#xff0c;以为是我的字段名字写错了&#xff0c;然而都是对的。 把错误的这个字段删了再打一遍就好了&#xff0c; 转载于:https://www.cnblogs.com/zxrxzw/p/10630164.html

[密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第9篇]香农对熵和信息的定义是什么?

这是计算机理论的最后一篇.我们讨论信息理论的基础概念,什么是香农定义的熵和信息. 熵 熵与确定性成反比 信息 密码学实例 [1] Thomas M. Cover and Joy A. Thomas. Elements of Information Theory ​ 2nd Edition. Wiley-Interscience, 2 edition, July 2006. [2] S. Vaj…

数据结构:(5)算法分析基础

算法时间复杂度分析 算法空间复杂度分析

蠢货别忘(一)common lisp funcall

自定义 cons&#xff0c;car&#xff0c;cdr Scheme 示例&#xff1a; (define (my_cons x y) (lambda (z) (z x y))) (define (my_car m) (m (lambda (p q) p))) (define (my_cdr m) (m (lambda (p q) p))) Common Lisp&#xff1a; (defun my_cons (x y) (lambda (z) (funcal…

[Leetcode][第35题][JAVA][搜索插入位置][二分法]

【问题描述】[中等] 【解答思路】 二分法 时间复杂度&#xff1a;O(LogN) 空间复杂度&#xff1a;O(1) public class Solution {public int searchInsert(int[] nums, int target) {int len nums.length;if (len 0) {return 0;}// 特判if (nums[len - 1] < target) {re…

数据结构:(6)其他情况的算法分析

最好&#xff0c;最坏,平均复杂度分析 递归算法的时间复杂度分析

树状数组的区间修改+查询

首先看树状数组是用来求前缀和比较方便的一种数据结构 sum[i] Sigma a[i] Sum(bit[x]&#xff09; 而区间修改也不难实现 就是引入一个差分数组del del[i]表示对i~n的修改 这样的话也就是最del[i]求前缀和 就能得到i~n的所有修改了 因为i前的每一个元素的修改都是对后面…