其他分享
首页 > 其他分享> > BZOJ1787/Luogu4281: [Ahoi2008]Meet 紧急集合

BZOJ1787/Luogu4281: [Ahoi2008]Meet 紧急集合

作者:互联网

画画图可知,三点\(lca\)必有两相同,\(a,b,c\)距离为\(dis_a + dis_b + dis_c - dis_{lca(a,b)} - dis_{lca(b,c)} - dis_{lca(a,c)}\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")

#else

#define D_e_Line ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 500007;

struct Edge{
    int nxt, pre, w;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w){
    e[++cntEdge] = (Edge){head[u], v, w}, head[u] = cntEdge;
}

int dis[N];
int fa[N], son[N], siz[N], dep[N];
inline void DFS_First(int u, int father){
    fa[u] = father, dep[u] = dep[father] + 1, siz[u] = 1;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v == father) continue;
        dis[v] = dis[u] + e[i].w;
        DFS_First(v, u);
        siz[u] += siz[v];
        if(!son[u] || siz[v] > siz[son[u]]){
            son[u] = v;
        }
    }
}
int dfn[N], dfnIdx, top[N];
inline void DFS_Second(int u, int ancester){
    top[u] = ancester, dfn[u] = ++dfnIdx;
    if(!son[u]) return;
    DFS_Second(son[u], ancester);
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v != son[u] && v != fa[u]){
            DFS_Second(v, v);
        }
    }
}
inline int LCA(int x, int y){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) Swap(x, y);
        x = fa[top[x]];
    }
    return dep[x] < dep[y] ? x : y;
}

int main(){
    int n, Ques;
    io >> n >> Ques;
    R(i,2,n){
        int u, v;
        io >> u >> v;
        add(u, v, 1);
        add(v, u, 1);
    }
    
    DFS_First(1, 0);
    DFS_Second(1, 1);
    
    while(Ques--){
        int a, b, c;
        io >> a >> b >> c;
        int A = LCA(a, b), B = LCA(b, c), C = LCA(a, c);
//      int disA = dis[a] + dis[b] - (dis[A] << 1);
//      int disB = dis[b] + dis[c] - (dis[B] << 1);
//      int disC = dis[a] + dis[c] - (dis[C] << 1);
        int pos;
        if(A == B) pos = C;
        else if(B == C) pos = A;
        else pos = B;
        printf("%d %d\n", pos, dis[a] + dis[b] + dis[c] - dis[A] - dis[B] - dis[C]);
    }
    
    return 0;
}

标签:Luogu4281,include,BZOJ1787,Ahoi2008,top,son,int,define,dis
来源: https://www.cnblogs.com/bingoyes/p/11218046.html