QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#279998#6848. City UpgradingTaylorFlyWA 114ms14672kbC++201.6kb2023-12-09 13:14:122023-12-09 13:14:13

Judging History

你现在查看的是最新测评结果

  • [2023-12-09 13:14:13]
  • 评测
  • 测评结果:WA
  • 用时:114ms
  • 内存:14672kb
  • [2023-12-09 13:14:12]
  • 提交

answer

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()

using i64 = long long;

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    std::vector<std::vector<int>> G(n);
    for (int i = 0; i < n - 1; i ++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    std::vector dp(n, std::vector<i64>(3));
    auto dfs = [&](auto self, int u, int fa) -> void {
        if (u == 0 && G[u].size() == 0 || u != 0 && G[u].size() == 1) {
            dp[u][0] = 0;
            dp[u][1] = a[u];
            dp[u][2] = 1e18;
            return ;
        }
        int ok = 0;
        for (int v: G[u]) {
            if (v == fa) continue;
            self(self, v, u);
            dp[u][0] += dp[v][2];
            dp[u][1] += std::min({dp[v][0], dp[v][1], dp[v][2]});
            if (dp[v][2] >= dp[v][1]) {
                ok = 1;
                dp[u][2] += dp[v][1];
            } else {
                dp[u][2] += dp[v][2];
            }
        }
        if (!ok) {
            i64 mn = 1e18;
            for (int v: G[u]) {
                if (v == fa) continue;
                mn = std::min(mn, dp[u][2] - dp[v][2] + dp[v][1]);
            }
            dp[u][2] = mn;
        }
        dp[u][1] += a[u];
    };
    dfs(dfs, 0, -1);
    std::cout << std::min(dp[0][1], dp[0][2]) << "\n";
}

int main() {
    std::ios::sync_with_stdio(false); 
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t --)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 114ms
memory: 14672kb

input:

1000
23
97976 19679 92424 30861 84737 62896 66360 54204 29129 13621 23631 61405 66883 53853 23079 66231 77727 88749 71092 97425 85117 79396 67781
1 2
2 3
1 4
1 5
5 6
1 7
5 8
3 9
9 10
5 11
8 12
9 13
3 14
4 15
6 16
9 17
8 18
3 19
8 20
11 21
3 22
19 23
3
93601 96295 41363
1 2
2 3
26
19405 8067 19601 81...

output:

419504
96295
334958
636478
114964
628081
464114
560260
479222
121326
64291
607551
278318
56546
413182
159607
313038
362098
635804
380900
594905
358972
325402
765893
705879
755158
206101
49049
7285
244319
208205
77015
623675
348208
515431
96136
607270
610292
656473
119999
320041
403718
80158
141749
4...

result:

wrong answer 999th lines differ - expected: '1176823366', found: '1553255927467255078'