zoukankan      html  css  js  c++  java
  • Least Common Ancestors

    /* Least Common Ancestors
     * Au: Small_Ash
     */
    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 500005, M = 1000005, MM = 20;
    
    int n, m, s, d[N], l[N], f[M], fn; // d 是深度,l 是最左边对应位置,f 是 dfs 序
    bool v[N]; // dfs 标记
    int head[N], nex[M], to[M], en; // 邻接表
    int r[M][MM]; // RMQ 用
    
    inline void add(int x, int y) {
        nex[++en] = head[x], head[x] = en, to[en] = y;
    }
    inline void push(int x) {
        f[fn] = x; fn++;
    }
    
    void rmq() {
        for (int i = 0; i < fn; i++) r[i][0] = f[i];
        for (int j = 1; (1 << j) <= fn; j++)
            for (int i = 0; i + (1 << j) - 1 < fn; i++) {
                if (d[r[i][j - 1]] <= d[r[i + (1 << (j - 1))][j - 1]]) r[i][j] = r[i][j - 1];
                else r[i][j] = r[i + (1 << (j - 1))][j - 1];
            }
    }
    
    void dfs(int x, int t) {
        v[x] = true; d[x] = t;
        l[x] = fn, push(x);
    
        for (int k = head[x]; k; k = nex[k]) {
            if (v[to[k]]) continue;
            dfs(to[k], t + 1);
            push(x);
        }
    }
    
    int lca(int  x, int y) {
        int k = 0; if (x > y) swap(x, y);
        int temp = y - x + 1;
        while ((1 << (k + 1)) <= temp) k++;
        if (d[r[x][k]] <= d[r[y - (1 << k) + 1][k]]) return r[x][k];
        else return r[y - (1 << k) + 1][k];
    }
    
    int main() {
        scanf("%d%d%d", &n, &m, &s);
        for (int i = 1, a, b; i < n; i++) {
            scanf("%d%d", &a, &b);
            add(a, b), add(b, a);
        }
    
        fn = 0;
        dfs(s, 0);
        rmq();
    
        for (int i = 1, a, b; i <= m; i++) {
            scanf("%d%d", &a, &b);
            printf("%d
    ", lca(l[a], l[b]));
        }
    
        return 0;
    }
    
    /**
     * Least Common Ancestors
     * std: [Luogu](https://www.luogu.org/problem/show?pid=3379)
     **/
    
    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 1000003;
    
    int n, rmq[N][23], t, x, y;
    
    int query(int l, int r) {
    	int k = int(log(r - l + 1) / log(2));
    	return max(rmq[l][k], rmq[r + 1 - (1 << k)][k]);
    }
    
    void st() {
    	for (int i = 1; i <= n; i++)
    		scanf("%d", &rmq[i][0]);
    	for (int j = 1; j <= int(log(n) / log(2)); j++)
    		for (int i = 1; i + (1 << j) - 1 <= n; i++)
    			rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
    }
    
    int main() {
    	scanf("%d%d", &n, &t);
    
    	st();
    
    	for (int i = 1; i <= t; i++) {
    		scanf("%d%d", &x, &y);
    		printf("%d
    ", query(x, y));
    	}
    	return 0;
    }
    

    Post author 作者: Grey
    Copyright Notice 版权说明: Except where otherwise noted, all content of this blog is licensed under a CC BY-NC-SA 4.0 International license. 除非另有说明,本博客上的所有文章均受 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 保护。
  • 相关阅读:
    光线步进——RayMarching入门
    MATLAB GUI制作快速入门
    Python中用绘图库绘制一条蟒蛇
    node 常见的一些系统问题
    webpack 入门指南
    利用 gulp 来合并seajs 的项目
    移动端 解决自适应 和 多种dpr (device pixel ratio) 的 [淘宝] 解决方案 lib-flexible
    富有哲理的文章
    NodeJS 难点(网络,文件)的 核心 stream 四: writable
    Vue.js 源码学习笔记 -- 分析前准备2 -- Object.defineProperty
  • 原文地址:https://www.cnblogs.com/greyqz/p/7688922.html
Copyright © 2011-2022 走看看