이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 100007;
const ll inf = 1e9;
const ll mod = 998244353;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector <int> adj[M];
pair <ll,ll> mx[M][105][2];
int n, v, a[M], who[M][105][2];
ll dp[M][105], ans;
void dfs(int node, int p)
{
ll sum = 0;
for(auto i : adj[node]) if(i != p) sum += a[i];
for(auto i : adj[node]) if(i != p){
dfs(i, node);
for(int j = 1; j <= v; ++j) dp[node][j] = max(dp[node][j], max(dp[i][j], dp[i][j - 1] + sum));
}
return;
}
void reroot(int node, int p)
{
ll sum = 0;
for(auto i : adj[node]) sum += a[i];
for(auto i : adj[node]) for(int j = 1; j <= v; ++j) dp[node][j] = max(dp[node][j], max(dp[i][j], dp[i][j - 1] + sum));
ans = max(ans, dp[node][v]);
for(auto i : adj[node]){
for(int j = 1; j <= v; ++j){
for(int k = 0; k < 2; ++k){
if(dp[i][j - k] > mx[node][j][k].F){
mx[node][j][k].S = mx[node][j][k].F;
mx[node][j][k].F = dp[i][j - k];
who[node][j][k] = i;
}
else if(dp[i][j - k] > mx[node][j][k].S) mx[node][j][k].S = dp[i][j - k];
}
}
}
for(auto i : adj[node]) if(i != p){
for(int j = 1; j <= v; ++j){
ll x[2] = {0};
for(int k = 0; k < 2; ++k){
if(who[node][j][k] == i) x[k] = mx[node][j][k].S;
else x[k] = mx[node][j][k].F;
}
dp[node][j] = max(x[0], x[1] + sum - a[i]);
}
reroot(i, node);
}
return;
}
int main()
{
scanf("%d%d", &n, &v);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i < n; ++i){
int x, y;
scanf("%d%d", &x, &y);
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1, 0);
reroot(1, 0);
printf("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
chase.cpp: In function 'int main()':
chase.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &n, &v);
| ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:69:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
chase.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |