結果
問題 | No.1221 木 *= 3 |
ユーザー |
![]() |
提出日時 | 2020-12-18 16:36:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 74 ms / 2,000 ms |
コード長 | 1,823 bytes |
コンパイル時間 | 1,713 ms |
コンパイル使用メモリ | 120,504 KB |
実行使用メモリ | 17,024 KB |
最終ジャッジ日時 | 2024-09-21 09:12:01 |
合計ジャッジ時間 | 4,009 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#pragma GCC target("avx2")#pragma GCC optimize("O3")#pragma GCC optimize("unroll-loops")#include <algorithm>#include <bitset>#include <climits>#include <cmath>#include <cstring>#include <deque>#include <forward_list>#include <iomanip>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <string>#include <unordered_map>#include <unordered_set>#include <utility>#include <vector>using namespace std;typedef long long ll;typedef pair<int, int> P;typedef bitset<16> BS;const ll MOD = 1E+09 + 7;const ll INF = 1E18;const int MAX_N = 1E+05;int N;ll A[MAX_N + 1], B[MAX_N + 1], dp[MAX_N + 1][2];vector<int> G[MAX_N + 1];ll tree(int par, int i, int exist);int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cin >> N;for (int i = 1; i <= N; i++) {cin >> A[i];}for (int i = 1; i <= N; i++) {cin >> B[i];}for (int i = 1; i <= N - 1; i++) {int from, to;cin >> from >> to;G[from].push_back(to);G[to].push_back(from);}fill(dp[1], dp[N + 1], -INF);/*for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";}}*/cout << max(tree(0, 1, 0), tree(0, 1, 1)) << "\n";return 0;}ll tree(int par, int i, int exist){if (dp[i][exist] != -INF)return dp[i][exist];ll res = 0;for (auto to : G[i]) {if (to != par) {if (exist) {res += max(tree(i, to, 0), tree(i, to, 1) + B[i] + B[to]);} else {res += max(tree(i, to, 0), tree(i, to, 1));}}}if (!exist)res += A[i];return dp[i][exist] = res;}