Submission #985574

# Submission time Handle Problem Language Result Execution time Memory
985574 2024-05-18T08:52:24 Z Alebn Chase (CEOI17_chase) C++14
30 / 100
216 ms 94800 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

int n, k, x, y;
vector<int> a, b;
vector<vector<int>> graph, dp;

void dfs(int i, int p) {
    for(auto j : graph[i]) {
        if(j != p) dfs(j, i);
    }
    for(int j = 0; j <= k; j++) {
        for(auto z : graph[i]) {
            if(z != p) {
                dp[i][j] = max(dp[i][j], max(dp[z][j], (j == k || dp[z][j + 1] == LLONG_MIN ? LLONG_MIN : dp[z][j + 1] + b[i] - (p == -1 ? 0 : a[p]))));
            }
        }
    }
}

signed main() {
    cin >> n >> k;
    a = vector<int>(n), b = vector<int>(n), graph = vector<vector<int>>(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n - 1; i++) {
        cin >> x >> y;
        x--, y--;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < graph[i].size(); j++) b[i] += a[graph[i][j]];
    }
    //
    dp = vector<vector<int>>(n, vector<int>(k + 1, LLONG_MIN));
    for(int i = 0; i < n; i++) dp[i][k] = 0;
    dfs(0, -1);
    //
    x = 0;
    for(int j = 0; j <= k; j++) x = max(x, dp[0][j]);
    cout << x << "\n";
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:34:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for(int j = 0; j < graph[i].size(); j++) b[i] += a[graph[i][j]];
      |                        ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 191 ms 94564 KB Output is correct
2 Correct 177 ms 94800 KB Output is correct
3 Correct 216 ms 92588 KB Output is correct
4 Correct 92 ms 15652 KB Output is correct
5 Correct 191 ms 92140 KB Output is correct
6 Correct 191 ms 92372 KB Output is correct
7 Correct 192 ms 92376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -