QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#279544#6848. City UpgradingTaylorFlyWA 109ms14608kbC++201.1kb2023-12-08 21:08:212023-12-08 21:08:21

Judging History

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

  • [2023-12-08 21:08:21]
  • 评测
  • 测评结果:WA
  • 用时:109ms
  • 内存:14608kb
  • [2023-12-08 21:08:21]
  • 提交

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>(2, 0));
    auto dfs = [&](auto self, int u, int fa) -> void {
        for (int v: G[u]) {
            if (v == fa) continue;
            self(self, v, u);
            int mn = 2e9;
            dp[u][1] += std::min(dp[v][1], dp[v][0]);
            dp[u][0] += dp[v][1];
        }
        dp[u][1] += a[u];
    };
    dfs(dfs, 0, -1);
    int ans = std::min(dp[0][1], dp[0][0]);
    if (n == 1) ans = dp[0][1];
    std::cout << ans << "\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: 109ms
memory: 14608kb

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:

451120
96295
393584
678918
114964
796814
512238
627433
626098
121326
90136
690477
278318
56546
456930
170095
362551
413611
825535
399702
766891
358972
422444
932206
832716
789705
211982
49049
7285
267080
237411
101498
785365
463372
573516
96136
787463
713667
671263
119999
406471
529347
80158
193760
...

result:

wrong answer 1st lines differ - expected: '419504', found: '451120'