QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#279584#6848. City UpgradingTaylorFlyWA 148ms24236kbC++201.7kb2023-12-08 21:35:512023-12-08 21:35:52

Judging History

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

  • [2023-12-08 21:35:52]
  • 评测
  • 测评结果:WA
  • 用时:148ms
  • 内存:24236kb
  • [2023-12-08 21:35:51]
  • 提交

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 (2, std::vector<i64>(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][0][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) {
            for (int v: G[u]) {
                if (v == fa) continue;
                dp[u][0][1] = std::min(dp[u][0][1], s - dp[v][0][1] + dp[v][1][1]);
            }
        }

        dp[u][1][1] += a[u];
    };
    dfs(dfs, 0, -1);
    std::cout << std::min(dp[0][1][1], dp[0][0][1]) << "\n";
}

int 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: 148ms
memory: 24236kb

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:

460329
41363
428595
757192
115309
626610
569654
529064
463515
143146
144832
762646
441796
60227
469178
159607
285561
413209
552018
499216
573315
417100
310889
806485
863910
793213
206101
97284
7285
245320
184392
65810
579628
414223
528078
96136
476101
604365
723896
84079
334185
400119
80158
146074
4...

result:

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