zoukankan      html  css  js  c++  java
  • 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 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.

    这题可以建树做,因为搜索树的话,它的中序遍历就是排好序的。
    但是这题可以用更巧妙的方法做,将图画出来,可以看出来,如果ans是x和y的最近公共祖先
    那么ans是介于x和y之间的【min(x,y), max(x,y)】

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int n,m,x,y,ans,an[100005];
     4 map<int,int> mp;
     5 int main(){
     6     cin >> n >> m;
     7     for(int i = 1; i <= m; i++){
     8         cin >> an[i];
     9         mp[an[i]] = 1;
    10     }
    11     for(int i = 0 ; i < n; i++){
    12         cin >> x >> y;
    13         for(int j = 1; j<= m; j++){
    14             ans = an[j];
    15             if((ans >= x&&ans <= y)||(ans>=y && ans <= x))
    16                 break;
    17         }
    18         if(mp[x] == 0 && mp[y] == 0)
    19             printf("ERROR: %d and %d are not found.
    ", x,y);
    20         else if(mp[x] == 0)
    21             printf("ERROR: %d is not found.
    ", x);
    22         else if(mp[y] == 0)
    23             printf("ERROR: %d is not found.
    ", y);
    24         else if(ans==x || ans==y)
    25             printf("%d is an ancestor of %d.
    ", ans==x?x:y,ans==x?y:x);
    26         else
    27             printf("LCA of %d and %d is %d.
    ", x,y,ans);
    28     }
    29     return 0;
    30 }





  • 相关阅读:
    禁用网络连接后无法访问本机数据库的问题
    DevExpress使用笔记
    DEV控件 皮肤问题
    SQLServer2008导入Excel遇到的问题
    InnoSetup使用笔记
    SQLServer清空数据库中所有表的数据
    MS SQL Server2000转换成MySQL
    由MySQL登录不了引发的一些问题
    onerror事件
    DIV+CSS一些小小的技巧
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11277414.html
Copyright © 2011-2022 走看看