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 }
  • 相关阅读:
    理解Linux系统负荷 和 ubuntu静态、动态设置ip地址
    Kafka笔记--监控系统KafkaOffsetMonitor
    Kafka笔记--使用ubuntu为bocker(服务器)windows做producer和comsumer(客户端)
    Android学习笔记--Broadcast, BroadcastReceiver(广播)
    Java学习笔记--xml构造与解析之Sax的使用
    Android学习笔记--存储方案(SharedPreference、文件IO)
    Android学习笔记--Menu菜单的使用
    Android学习笔记--AlertDialog应用
    Kafka笔记--参数说明及Demo
    Kafka笔记--分布式环境搭建
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11273877.html
Copyright © 2011-2022 走看看