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 国际许可协议 保护。
  • 相关阅读:
    数据绑定表达式语法(Eval,Bind区别)
    使用博客园的第一件事 自定义主题
    sql2000 跨服务器复制表数据
    使用UpdatePanel 局部刷新出现中文乱码的解决方法!!
    MMC不能打开文件MSC文件
    sql 日期 、时间相关
    loaded AS2 swf call function in AS3 holder
    Rewrite the master page form action attribute in asp.net 2.0
    100万个不重复的8位的随机数
    flash 中实现斜切变型
  • 原文地址:https://www.cnblogs.com/greyqz/p/7688922.html
Copyright © 2011-2022 走看看