이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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));
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";
}
컴파일 시 표준 에러 (stderr) 메시지
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |