zoukankan      html  css  js  c++  java
  • 1151 LCA in a Binary Tree (30 分)

    1151 LCA in a Binary Tree (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.

    Given any two nodes in a binary tree, 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 binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. 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 binary tree, 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
    7 2 3 4 6 5 1 8
    5 3 7 2 6 4 8 1
    2 6
    8 1
    7 9
    12 -3
    0 8
    99 99
    

    Sample Output:

    LCA of 2 and 6 is 3.
    8 is an ancestor of 1.
    ERROR: 9 is not found.
    ERROR: 12 and -3 are not found.
    ERROR: 0 is not found.
    ERROR: 99 and 99 are not found.
    
     
    挺有意思的,我是先建树了,还有到了类似的标记数组来做到
    算是LCA的小题了,也挺容易理解的。
     
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 int n,m;
     5 int mid[10005], pre[10005], fa[10005];
     6 struct Node{
     7     int val, depth;
     8 };
     9 Node v[1000000];
    10 int pos = 1;
    11 map<int,int> mp;
    12 
    13 void buildtree(int an, int dep, int L, int R){
    14     int ans = -1;
    15     for(int i = L; i <= R; i++){
    16         if(mid[i] == pre[an]){
    17             ans = i;
    18             break;
    19         }
    20     }
    21     v[pos].val = pre[an],v[pos].depth = dep;
    22     mp[pre[an]] = pos;
    23     int kk = pos;
    24     pos++;
    25     if(ans > L){
    26         fa[pre[an+1]] = kk;
    27         buildtree(an+1, dep+1, L, ans-1);
    28     }
    29 
    30     if(ans < R){
    31         fa[pre[an+(ans-L)+1]] = kk;
    32         buildtree(an+(ans-L)+1, dep+1, ans+1, R);
    33     }
    34 }
    35 
    36 int main(){
    37     cin >> n >> m;
    38     for(int i = 0; i < m; ++i) cin >> mid[i];
    39     for(int i = 0; i < m; ++i) cin >> pre[i];
    40     buildtree(0,1,0,m-1);
    41     // for(int i = 0 ; i < pos; i++){
    42     //     cout << v[i].val <<" "<<v[i].depth<<endl;
    43     // }
    44     int x,y;
    45     while(n--){
    46         cin >> x >> y;
    47         if(mp[x] == 0 && mp[y] == 0){
    48             printf("ERROR: %d and %d are not found.
    ", x,y);
    49         }else if(mp[x] == 0){
    50             printf("ERROR: %d is not found.
    ", x);
    51         }else if(mp[y] == 0){
    52             printf("ERROR: %d is not found.
    ", y);
    53         }else{
    54             int xx = mp[x];
    55             int yy = mp[y];
    56             bool flag1 = true, flag2 = true;
    57             while(v[xx].val != v[yy].val){
    58                 if(v[xx].depth > v[yy].depth){
    59                     xx = fa[v[xx].val];
    60                     flag1 = false;
    61                 }else if(v[yy].depth > v[xx].depth){
    62                     yy = fa[v[yy].val];
    63                     flag2 = false;
    64                 }else{
    65                     xx = fa[v[xx].val];
    66                     yy = fa[v[yy].val];
    67                     flag1 = false;
    68                     flag2 = false;
    69                 }
    70             }
    71             if(!flag1 && !flag2){
    72                 printf("LCA of %d and %d is %d.
    ", x,y,v[xx].val);
    73             }else if(!flag2){
    74                 printf("%d is an ancestor of %d.
    ", x,y);
    75             }else if(!flag1){
    76                 printf("%d is an ancestor of %d.
    ", y,x);
    77             }else{
    78                 printf("%d is an ancestor of %d.
    ", x,y);
    79             }
    80         }
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    SDL2 gif动态图加载
    js动态创建svg与use 使用iconfont symbol
    纯css3实现轮播图
    前端面试题
    移动端开发的兼容性问题
    JS高级(五)--ES6(Promise、async、类)
    JS高级(四)--ES6(常用的、重点的)
    JS高级(三)--原型链、闭包、作用域、函数的四种调用方式
    JS高级(二)--继承
    JS高级(一)--基础、数据类型、对象的基本使用、构造函数
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11273877.html
Copyright © 2011-2022 走看看