TB椰程 TypeBuddy 打字搭子

2023-12 · 8级 · 大量的工作沟通

GESP 标程 · 考级真题 · 代码 · cpp · 难度 5/5 · 共 1650 字

CCF 原卷官方标程 · 数组与向量、字符串、STL、位运算 · 本地编译通过

  • 2023-12
  • 数组与向量

正文

// GESP 2023-12 C++ 8级 编程题 2 · 大量的工作沟通
// 原题:https://oj.yecheng.tv/p/gs2312l8p2
// 官方原卷:https://gesp.ccf.org.cn/101/attach/1584918480027680.pdf
// 题意:某公司有名员工,编号从至 。
// 知识点:数组与向量、字符串、STL、位运算
// 参考程序:CCF 原卷官方标程,PDF 抽取后已去行号;本地编译核验通过,供打字练习
// 原卷样例验证:样例验证 2/2 通过
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int N = 100005;
int fa[N], sz[N], dep[N], son[N], tp[N], mxId[N];
int cnt, fir[N], tar[N], nxt[N];
void link(int a, int b) {
    tar[++ cnt] = b, nxt[cnt] = fir[a], fir[a] = cnt;
}
void dfs(int x, int mxid) {
    int Mx = 0; sz[x] = 1;
    mxId[x] = max(x, mxid);
    for (int i = fir[x]; i; i = nxt[i]) {
        dep[tar[i]] = dep[x] + 1;
        dfs(tar[i], mxId[x]);
        sz[x] += sz[tar[i]];
        if (Mx < sz[tar[i]])
            Mx = sz[son[x] = tar[i]];
    }
}
void gettp(int x) {
    tp[x] = x;
    if (son[fa[x]] == x)
        tp[x] = tp[fa[x]];
    for (int i = fir[x]; i; i = nxt[i])
        gettp(tar[i]);
}
int lca(int x,int y){
    while (tp[x] != tp[y])
        dep[tp[x]] > dep[tp[y]] ? x = fa[tp[x]] : y = fa[tp[y]];
    return dep[x] < dep[y] ? x : y;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 2; i <= n; i ++)
        scanf("%d", &fa[i]), link(++ fa[i], i);
    dfs(1, 1), gettp(1);
    int q;
    scanf("%d", &q);
    while(q --) {
        int m, x, y;
        scanf("%d%d", &m, &x), x ++;
        for(int i = 2; i <= m; i ++)
            scanf("%d", &y), x = lca(x, ++ y);
        cout << mxId[x] - 1 << endl;
    }
    return 0;
}

GESP 标程 · 考级真题的其它内容

打字首页 · 英语学习中心 · 词库画廊 · 编程打字 · 指法入门 · 天梯榜 · 数据分析 · 班级课堂 · 关于我们
椰程 TypeBuddy 打字搭子 —— 键盘指法练习 · 单词记忆 · 班级课堂 · 在线 PK