zoukankan      html  css  js  c++  java
  • 洛谷P4281 紧急集合 / 聚会

    LCA

    题目要求找离三个点最近的点,我们先看两个点的情况,自然是找LCA,那么三个点的时候是否与LCA有关呢?
    显然,离三个点最近的点一定是在这三个点联通的简单路径上。
    可以简单证明一下,假设某个点离a,b,c三个点最近且不在联通这三个点的简单路径上,那么有a,b,c中有两个点一定会经过某个点才能来到该点,换句话说,就是有两个人都要多走一段距离,那为什么不把两个人多走的距离换成让另外一个人走呢?这样显然更优。
    而且我们的候选点一定在某两个点的LCA上,同样可以假设改点不在LCA上,那么也可以假设成两个人多走的距离用一个人走来替换,这样我们来到的点又变成了LCA。
    再有三个点中每两个点的LCA有三对,必定有两对会重合(三个点的路径只会有两个交点),我们可以发现前面描述的两个点就是这两个不同的LCA。
    因此我们的最优点就在不被重合的那个LCA上。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 500005;
    int n, m, cnt, t, head[N], depth[N], p[N][20];
    struct Edge{ int v, next; }edge[N<<1];
    
    void addEdge(int a, int b){
        edge[cnt].v = b, edge[cnt].next = head[a], head[a] = cnt ++;
    }
    
    void dfs(int s, int fa){
        depth[s] = depth[fa] + 1;
        p[s][0] = fa;
        for(int i = 1; i <= t; i ++) p[s][i] = p[p[s][i - 1]][i - 1];
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(u == fa) continue;
            dfs(u, s);
        }
    }
    
    int lca(int x, int y){
        if(depth[x] < depth[y]) swap(x, y);
        for(int i = t; i >= 0; i --){
            if(depth[p[x][i]] >= depth[y]) x = p[x][i];
        }
        if(x == y) return y;
        for(int i = t; i >= 0; i --){
            if(p[x][i] != p[y][i]) x = p[x][i], y = p[y][i];
        }
        return p[y][0];
    }
    
    int main(){
    
        full(head, -1);
        n = read(), m = read();
        t = (int)(log(n) / log(2)) + 1;
        for(int i = 0; i < n - 1; i ++){
            int u = read(), v = read();
            addEdge(u, v), addEdge(v, u);
        }
        depth[0] = -1, dfs(1, 0);
        while(m --){
            int a = read(), b = read(), c = read();
            int x = lca(a, b), y = lca(b, c), z = lca(a, c);
            int tmp = 0;
            if(x == y) tmp = z; else if(x == z) tmp = y; else if(y == z) tmp = x;
            printf("%d %d
    ", tmp, depth[a] + depth[b] + depth[c] - depth[x] - depth[y] - depth[z]);
        }
        return 0;
    }
    
  • 相关阅读:
    webpack4系列之 【2. 踩坑--webpack 2.x升级至4.x】
    计算月份差方法封装
    局域网内访问另一台电脑上运行的代码
    npm遇到的问题--npm install 执行报错 /bin/git submodule update -q --init --recursive
    功能说明书
    第一次结对作业
    MathExam
    第一次作业
    2017《Java预备作业》02 计科1501 李晓燕
    2017《Java技术》预备作业 计科1501 李晓燕
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10638333.html
Copyright © 2011-2022 走看看