• A1151 LCA in a Binary Tree (30分)


    一、技术总结

    二、参考代码

    #include <iostream>
    #include <vector>
    #include <map>
    using namespace std;
    map<int, int> pos;
    vector<int> in, pre;
    void lca(int inl, int inr, int preRoot, int a, int b) {
        if (inl > inr) return;
        int inRoot = pos[pre[preRoot]], aIn = pos[a], bIn = pos[b];
        if (aIn < inRoot && bIn < inRoot)
            lca(inl, inRoot-1, preRoot+1, a, b);
        else if ((aIn < inRoot && bIn > inRoot) || (aIn > inRoot && bIn < inRoot))
            printf("LCA of %d and %d is %d.
    ", a, b, in[inRoot]);
        else if (aIn > inRoot && bIn > inRoot)
            lca(inRoot+1, inr, preRoot+1+(inRoot-inl), a, b);
        else if (aIn == inRoot)
                printf("%d is an ancestor of %d.
    ", a, b);
        else if (bIn == inRoot)
                printf("%d is an ancestor of %d.
    ", b, a);
    }
    int main() {
        int m, n, a, b;
        scanf("%d %d", &m, &n);
        in.resize(n + 1), pre.resize(n + 1);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &in[i]);
            pos[in[i]] = i;
        }
        for (int i = 1; i <= n; i++) scanf("%d", &pre[i]);
        for (int i = 0; i < m; i++) {
            scanf("%d %d", &a, &b);
            if (pos[a] == 0 && pos[b] == 0)
                printf("ERROR: %d and %d are not found.
    ", a, b);
            else if (pos[a] == 0 || pos[b] == 0)
                printf("ERROR: %d is not found.
    ", pos[a] == 0 ? a : b);
            else
                lca(1, n, 1, a, b);
        }
        return 0;
    }
    
  • 相关阅读:
    数据一致性
    linux下关于程序性能和系统性能的工具、方法
    关于网站架构的一些文章
    列出man手册所有函数的方法
    dup和dup2函数以及管道的实现
    knowledge about apache
    第三篇 读后感
    课程作业02
    《大道至简》第二篇读后感
    课程作业01
  • 原文地址:https://www.cnblogs.com/tsruixi/p/13189393.html
Copyright © 2020-2023  润新知