比赛 CSP2022提高组 评测结果 AWAWWAAAAWWTTTTTTTTTTTTTT
题目名称 数据传输 最终得分 24
用户昵称 lihaoze 运行时间 43.699 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-10-30 12:29:20
显示代码纯文本
#include "bits/stdc++.h"

using i64 = long long;

const int N = 2e5 + 10;
int n, q, k;
int fa[N];
i64 a[N], f[N];
std::vector<int> to[N];

void dfs(int u){
    if (k == 1) {
        f[u] = a[u] + f[fa[u]];
    } else if (k == 2) {
        f[u] = a[u] + std::min(f[fa[u]],f[fa[fa[u]]]);
    } else if (k == 3) {
        f[u] = a[u] + std::min({f[fa[u]],f[fa[fa[u]]],f[fa[fa[fa[u]]]]});
    }
    if (fa[u] == 0) {
        f[u] = a[u];
    }
    for (int v : to[u]) {
        if (v != fa[u]) {
            fa[v] = u,
            dfs(v);
        }
    }
}

int main(){
    freopen("csp2022_transmit.in", "r", stdin); 
    freopen("csp2022_transmit.out", "a", stdout);
    std::cin >> n >> q >> k;
    for (int i = 1; i <= n; ++ i) {
        std::cin >> a[i];
    }
    for (int i=1; i < n; ++ i) {
        int x, y;
        std::cin >> x >> y;
        to[x].push_back(y),
        to[y].push_back(x);
    }
    while(q --) {
        memset(f, 0, sizeof f);
        memset(fa, 0, sizeof fa);
        int x, y;
        std::cin >> x >> y;
        f[0] = 1LL << 63 - 1;
        dfs(x);
        std::cout << f[y] << '\n';
    }
    return 0;
}