洛谷 P10113:[GESP202312 八级] 大量的工作沟通 ← 倍增+邻接表 or 树链剖分+邻接表

【题目来源】
https://www.luogu.com.cn/problem/P10113
https://oj.czos.cn/p/3492
https://bas.ssoier.cn/problem_show.php?pid=4040

【题目描述】
某公司有 N 名员工,编号从 0 至 N-1。其中,除了 0 号员工是老板,其余每名员工都有一个直接领导。我们假设编号为 i 的员工的直接领导是 fi。
该公司有严格的管理制度,每位员工只能受到本人或直接领导或间接领导的管理。具体来说,规定员工 x 可以管理员工 y,当且仅当 x=y,或 x=fy,或 x 可以管理 fy。特别地,0 号员工老板只能自我管理,无法由其他任何员工管理。
现在,有一些同事要开展合作,他们希望找到一位同事来主持这场合作,这位同事必须能够管理参与合作的所有同事。如果有多名满足这一条件的员工,他们希望找到编号最大的员工。你能帮帮他们吗?

【输入格式】
第一行一个整数 N,表示员工的数量。
第二行 N-1 个用空格隔开的正整数,依次为 f1,f2,…,f_{N-1}。
第三行一个整数 Q,表示共有 Q 场合作需要安排。
接下来 Q 行,每行描述一场合作:开头是一个整数 m(2≤m≤N),表示参与本次合作的员工数量;接着是 m 个整数,依次表示参与本次合作的员工编号(保证编号合法且不重复)。
保证公司结构合法,即不存在任意一名员工,其本人是自己的直接或间接领导。

【输出格式】
输出 Q 行,每行一个整数,依次为每场合作的主持人选。​​​​​​​

【输入样例一】
5
0 0 2 2
3
2 3 4
3 2 3 4
2 1 4​​​​​​​

【输出样例一】
2
2
0

【输入样例二】
7
0 1 0 2 1 2
5
2 4 6
2 4 5
3 4 5 6
4 2 4 5 6
2 3 4​​​​​​​

【输出样例二】
2
1
1
1
0

【样例一解释】
对于第一场合作,员工 3,4 有共同领导 2 ,可以主持合作。
对于第二场合作,员工 2 本人即可以管理所有参与者。
对于第三场合作,只有 0 号老板才能管理所有员工。

【数据范围】
对于 25% 的测试点,保证 N≤50。
对于 50% 的测试点,保证 N≤300。
对于所有测试点,保证 3≤N≤10^5,Q≤100,m≤10^4。

【算法分析】
● 树上倍增:https://blog.csdn.net/hnjzsyjyj/article/details/156747410

●​​​​​​​ 树链剖分:https://blog.csdn.net/hnjzsyjyj/article/details/156956145

● 树链剖分(Heavy-Light Decomposition,HLD)是一种将树结构转化为线性序列的算法技巧,常用于解决树上路径查询与修改‌以及‌子树查询与修改‌这两大类问题。
其主要包括两个分解步骤:
(1)重链剖分‌:将树的边分为“重边”和“轻边”。对于每个非叶子节点,选择其所有子节点中‌子树大小最大‌的那一个,连接这两点的边即为重边,连接到其他子节点的边则为轻边。由重边相连形成的路径称为重链。
(2)DFS 序重排‌:通过一次深度优先搜索,优先遍历重儿子,从而保证‌每条重链上的节点在新的 DFS 序中是连续存储的‌。

● 树链剖分的核心思想是通过两次 DFS 对树进行剖分,将树分解为若干条“重链”,并重新安排节点的访问顺序(DFS 序),使得每条重链上的节点在序列中连续存储,同时每个子树内的节点也连续存储。这样,复杂的树形操作就被转化为了对线性序列的区间操作,可以借助线段树、树状数组等数据结构高效地完成。

● 树链剖分的几个重要概念
(1)重儿子 (Heavy Son)‌:对于树中的一个非叶子节点,其所有子节点中,‌子树大小(含子树的根的节点数)最大‌的那个子节点,称为该节点的重儿子。如果存在多个子节点子树大小相同,可任意选取其中一个作为重儿子。
(2)轻儿子 (Light Son)‌:非叶子节点的所有子节点中,除了重儿子以外的其他子节点,都称为该节点的轻儿子。
(3)重边 (Heavy Edge)‌:连接一个节点与其重儿子的边,称为重边。
(4)轻边 (Light Edge)‌:连接一个节点与其轻儿子的边,称为轻边。
(5)重链 (Heavy Path)‌:由一系列连续的重边首尾相连形成的路径,称为一条重链。整棵树可以被分解为若干条互不相交的重链。
(6)链头 (Head of Chain / Top)‌:每条重链的起始节点,即这条链上‌深度最浅‌的那个节点,称为该重链的链头。树根通常单独构成一条重链,其本身即为链头。

● 树链剖分的两个 dfs 函数
(1)dfs1 函数。求解数组 dep[]、pre[]、son[]、siz[]
int dep[N]; //dep[x] represents depth of node x
int pre[N]; //pre[x] represents parent node of node x
int son[N]; //son[x] represents heavy child of non-leaf node x
int siz[N]; //siz[x] represents number of nodes in the subtree rooted at node x
(2)dfs2 函数。求解数组 top[]
int top[N]; //top[x] represents head node of the heavy chain where node x is located

【算法代码一:树链剖分 + 邻接表

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5;
vector<int> tree[N];
int dep[N]; //dep[x] represents depth of node x
int pre[N]; //pre[x] represents parent node of node x
int son[N]; //son[x] represents heavy child of non-leaf node x
int siz[N]; //siz[x] represents number of nodes in the subtree rooted at node x
int top[N]; //top[x] represents head node of the heavy chain where node x is located
int pos[N]; //pos[x] represents the position of node x in the DFS sequence
int cur_pos; //The current position in the DFS sequence

void dfs1(int u,int fa) { //Solving for dep[],pre[],son[],siz[]
    pre[u]=fa;
    dep[u]=(fa==-1?0:dep[fa]+1);
    siz[u]=1;
    son[u]=-1; //-1 indicates no heavy son

    int mx_size=0;
    for(int j:tree[u]) {
        if(j==fa) continue;
        dfs1(j,u);
        siz[u]+=siz[j];
        if(siz[j]>mx_size) {
            mx_size=siz[j];
            son[u]=j;
        }
    }
}

void dfs2(int x, int topx) { //Solving for top[]
    top[x]=topx;
    pos[x]=cur_pos++;

    if(son[x]!=-1) {
        dfs2(son[x],topx);
    }

    for(int j:tree[x]) {
        if(j==pre[x] || j==son[x]) continue;
        dfs2(j,j); //thin son starts a new chain
    }
}

int LCA(int x, int y) { //cal LCA using Tree Chain Division
    while(top[x]!=top[y]) {
        if(dep[top[x]]<dep[top[y]]) {
            swap(x,y);
        }
        x=pre[top[x]];
    }
    return dep[x]<dep[y]?x:y;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;
    for(int i=1; i<n; i++) {
        int x;
        cin>>x;
        tree[x].push_back(i);
        tree[i].push_back(x);
    }

    //Initialize Tree Chain Division
    cur_pos=0;
    dfs1(0,-1);
    dfs2(0,0);

    int T;
    cin>>T;
    while(T--) {
        int m,idx;
        cin>>m>>idx;
        int ans=idx;
        for(int i=1; i<m; i++) {
            int t;
            cin>>t;
            ans=LCA(ans,t);
        }
        cout<<ans<<'\n';
    }

    return 0;
}

/*
in:
7
0 1 0 2 1 2
5
2 4 6
2 4 5
3 4 5 6
4 2 4 5 6
2 3 4

out:
2
1
1
1
0
*/

【算法代码二:倍增 + 邻接表

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5;
const int LOG=20;
int f[N][LOG+5];
vector<int> tree[N];
int dep[N];

void dfs(int u,int fa) {
    f[u][0]=fa;
    dep[u]=dep[fa]+1;
    for(int k=1; k<=LOG; k++) {
        f[u][k]=f[f[u][k-1]][k-1];
    }
    for(int j:tree[u]) {
        if(j!=fa) dfs(j,u);
    }
}

int LCA(int x,int y) {
    if(dep[x]<dep[y]) swap(x,y);
    int dif=dep[x]-dep[y];
    for(int k=0; k<=LOG; k++) {
        if(dif & (1<<k)) x=f[x][k];
    }
    if(x==y) return x;

    for(int k=LOG; k>=0; k--) {
        if(f[x][k]!=f[y][k]) {
            x=f[x][k];
            y=f[y][k];
        }
    }
    return f[x][0];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin>>n;
    for(int i=1; i<n; i++) {
        int x;
        cin>>x;
        tree[x].push_back(i);
        tree[i].push_back(x);
    }

    dep[0]=0;
    dfs(0,0);

    int T;
    cin>>T;
    while(T--) {
        int m,idx;
        cin>>m>>idx;
        int ans=idx;
        for(int i=1; i<m; i++) {
            int t;
            cin>>t;
            ans=LCA(ans,t);
        }
        cout<<ans<<'\n';
    }
    return 0;
}

/*
in:
7
0 1 0 2 1 2
5
2 4 6
2 4 5
3 4 5 6
4 2 4 5 6
2 3 4

out:
2
1
1
1
0
*/




【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/156956145
https://gesp.ccf.org.cn/101/attach/1584918480027680.pdf
https://ti.luogu.com.cn/problemset/
https://blog.csdn.net/weixin_60445850/article/details/147918344
https://www.luogu.com.cn/problem/solution/P10113




 

posted @ 2026-01-15 21:43  Triwa  阅读(4)  评论(0)    收藏  举报