QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#279688#6848. City UpgradingTaylorFlyWA 153ms25136kbC++201.7kb2023-12-08 23:08:482023-12-08 23:08:50

Judging History

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

  • [2023-12-08 23:08:50]
  • 评测
  • 测评结果:WA
  • 用时:153ms
  • 内存:25136kb
  • [2023-12-08 23:08:48]
  • 提交

answer

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

#define int 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 (2, std::vector<int>(2)));
    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] = 0;
            dp[u][1][1] = a[u];
            dp[u][0][1] = 1e18;
            return ;
        }
        int ok = 0;
        for (int v: G[u]) {
            if (v == fa) continue;
            self(self, v, u);
            dp[u][1][1] += std::min({dp[v][1][1], dp[v][0][0], dp[v][0][1]});
            if (dp[v][0][1] >= dp[v][1][1]) {
                ok = 1;
                dp[u][0][1] += dp[v][1][1];
            } else {
                dp[u][0][1] += dp[v][0][1];
            }
            dp[u][0][0] += dp[v][0][1];
        }
        int s = dp[u][0][1];
        if (!ok) {
            int mn = 1e18;
            for (int v: G[u]) {
                if (v == fa) continue;
                mn = std::min(mn, s - dp[v][0][1] + dp[v][1][1]);
            }
            dp[u][0][1] = mn;
        }
        dp[u][1][1] += a[u];
    };
    dfs(dfs, 0, -1);
    std::cout << std::min(dp[0][1][1], dp[0][0][1]) << "\n";
}

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

详细

Test #1:

score: 0
Wrong Answer
time: 153ms
memory: 25136kb

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'