Loading

洛谷-P3379 【模板】最近公共祖先(LCA)

【模板】最近公共祖先(LCA)

倍增 lca 模板

真心觉得二进制太奇妙了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
const int maxn = 5e5 + 10;
vector<int>gra[maxn];
int fa[maxn][25], dep[maxn];
int n, m, s;

void dfs(int now, int pre, int cur)
{
    if(dep[now]) return;
    dep[now] = cur;
    fa[now][0] = pre;
    for(int i=0; i<gra[now].size(); i++)
    {
        int nex = gra[now][i];
        if(nex == pre) continue;
        dfs(nex, now, cur + 1);
    }
}

void init_lca()
{
    for(int i=1; i<=20; i++)
        for(int j=1; j<=n; j++)
            fa[j][i] = fa[fa[j][i-1]][i-1];
}

int lca(int a, int b)
{
    if(dep[a] < dep[b]) swap(a, b);
    int dif = dep[a] - dep[b];
    for(int i=20; i>=0; i--)
    {
        if(dif >= (1 << i))
        {
            a = fa[a][i];
            dif -= 1 << i;
        }
    }
    if(a == b) return a;
    for(int i=20; i>=0; i--)
    {
        if(fa[a][i] != fa[b][i])
        {
            a = fa[a][i];
            b = fa[b][i];
        }
    }
    return fa[a][0];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> s;
    for(int i=1; i<n; i++)
    {
        int x, y;
        cin >> x >> y;
        gra[x].push_back(y);
        gra[y].push_back(x);
    }
    dfs(s, s, 1);
    init_lca();
    while(m--)
    {
        int x, y;
        cin >> x >> y;
        cout << lca(x, y) << endl;
    }
    return 0;
}

树链剖分 LCA 模板

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 5e5 + 10;
int dep[maxn], fa[maxn], siz[maxn], hson[maxn];
int top[maxn], dfn[maxn], rnk[maxn];
vector<int>gra[maxn];

void dfs1(int now, int cnt)
{
    siz[now] = 1;
    hson[now] = 0;
    dep[now] = cnt;
    for(int i=0; i<gra[now].size(); i++)
    {
        int nex = gra[now][i];
        if(nex == fa[now]) continue;
        fa[nex] = now;
        dfs1(nex, cnt + 1);
        siz[now] += siz[nex];
        if(siz[hson[now]] < siz[nex])
            hson[now] = nex;
    }
}

int tp = 0;
void dfs2(int now, int t)
{
    top[now] = t;
    tp++;
    dfn[now] = tp;
    rnk[tp] = now;
    if(hson[now])
    {
        dfs2(hson[now], t);
        for(int i=0; i<gra[now].size(); i++)
        {
            int nex = gra[now][i];
            if(fa[now] != nex && hson[now] != nex)
                dfs2(nex, nex);
        }
    }
}

void init(int n, int rt)
{
    tp = 0;
    for(int i=0; i<=n; i++) top[i] = i;
    dfs1(rt, 1);
    dfs2(rt, rt);
}

int LCA(int a, int b)
{
    while(top[a] != top[b])
    {
        if(dep[top[a]] < dep[top[b]]) swap(a, b);
        a = fa[top[a]];
    }
    return dep[a] < dep[b] ? a : b;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, s;
    cin >> n >> m >> s;
    for(int i=1; i<n; i++)
    {
        int x, y;
        cin >> x >> y;
        gra[x].push_back(y);
        gra[y].push_back(x);
    }
    init(n, s);
    while(m--)
    {
        int a, b;
        cin >> a >> b;
        cout << LCA(a, b) << '\n';
    }
    return 0;
}
posted @ 2022-07-03 14:46  dgsvygd  阅读(41)  评论(0编辑  收藏  举报