結果
| 問題 |
No.1221 木 *= 3
|
| コンテスト | |
| ユーザー |
f_stsh
|
| 提出日時 | 2020-09-06 12:24:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,652 bytes |
| コンパイル時間 | 2,418 ms |
| コンパイル使用メモリ | 187,272 KB |
| 実行使用メモリ | 818,176 KB |
| 最終ジャッジ日時 | 2024-11-29 07:01:25 |
| 合計ジャッジ時間 | 22,233 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 3 MLE * 15 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int N;
vector<ll> a, b;
vector<vector<int>> G;
vector<vector<ll>> dp(2, vector<ll>(MAX, -LINF));
vector<bool> used;
vector<int> root;
/* function */
void init() {
cin >> N;
a.resize(N);
b.resize(N);
G.resize(N, vector<int>(N));
for (int i = 0; i < N; i++) cin >> a[i];
for (int i = 0; i < N; i++) cin >> b[i];
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].push_back(v);
G[v].push_back(u);
}
}
void dfs(int x) {
if (used[x]) return ;
used[x] = true;
// transition
// dp[0][i] = a[i] + sum_{j = i.child} [max(dp[0][j], dp[1][j])]
// dp[1][i] = sum_{j = i.child}[dp[0][j], dp[1][j] + b[i] + b[j]]
dp[0][x] = a[x];
dp[1][x] = 0;
for (int y : G[x]) {
if (used[y]) continue;
dfs(y);
dp[0][x] += max(dp[0][y], dp[1][y]);
dp[1][x] += max(dp[0][y], dp[1][y] + b[x] + b[y]);
}
}
void solve() {
// dp[0][i]: 頂点iを根とする部分木について,頂点iを消した場合のスコアの最大値
// dp[1][i]: 頂点iを根とする部分木について,頂点iを残した場合のスコアの最大値
dp.assign(2, vector<ll>(N, 0));
used.assign(N, false);
dfs(0);
cout << max(dp[0][0], dp[1][0]) << '\n';
}
/* main */
int main(){
init();
solve();
}
f_stsh