結果
問題 | No.1221 木 *= 3 |
ユーザー | yakki |
提出日時 | 2020-09-06 11:32:31 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 153 ms / 2,000 ms |
コード長 | 1,494 bytes |
コンパイル時間 | 1,416 ms |
コンパイル使用メモリ | 123,664 KB |
実行使用メモリ | 19,972 KB |
最終ジャッジ日時 | 2023-08-19 13:44:57 |
合計ジャッジ時間 | 5,161 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,876 KB |
testcase_01 | AC | 3 ms
4,872 KB |
testcase_02 | AC | 3 ms
5,000 KB |
testcase_03 | AC | 2 ms
4,988 KB |
testcase_04 | AC | 3 ms
4,940 KB |
testcase_05 | AC | 2 ms
5,008 KB |
testcase_06 | AC | 3 ms
5,064 KB |
testcase_07 | AC | 153 ms
19,972 KB |
testcase_08 | AC | 149 ms
19,716 KB |
testcase_09 | AC | 149 ms
19,848 KB |
testcase_10 | AC | 152 ms
19,636 KB |
testcase_11 | AC | 151 ms
19,580 KB |
testcase_12 | AC | 138 ms
11,136 KB |
testcase_13 | AC | 130 ms
11,468 KB |
testcase_14 | AC | 135 ms
11,304 KB |
testcase_15 | AC | 135 ms
11,332 KB |
testcase_16 | AC | 134 ms
11,500 KB |
testcase_17 | AC | 145 ms
10,988 KB |
testcase_18 | AC | 136 ms
10,808 KB |
testcase_19 | AC | 143 ms
10,984 KB |
testcase_20 | AC | 144 ms
11,004 KB |
testcase_21 | AC | 140 ms
11,024 KB |
ソースコード
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<bitset> #include<set> #include<map> #include<stack> #include<queue> #include<deque> #include<list> #include<iomanip> #include<cmath> #include<cstring> #include<functional> #include<cstdio> #include<cstdlib> #include<numeric> using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 //#define MOD 1000000007 #define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 const double EPS = 1e-10; using ll = long long; using Pi = pair<int,int>; using Pl = pair<ll,ll>; int n; vector<int> a,b; vector<vector<int>> G; ll dp[100010][2]; ll dfs(int u, int k, int p){ if(p != -1 && G[u].size() == 1){ if(k == 0) return a[u]; else return 0; } if(dp[u][k] != -LINF) return dp[u][k]; ll res = 0; if(k == 0) res = a[u]; for(int v : G[u]){ if(v == p) continue; if(k == 0){ res += max(dfs(v,0,u),dfs(v,1,u)); } else res += max(dfs(v,0,u),dfs(v,1,u)+b[u]+b[v]); } return dp[u][k] = res; } int main(){ cin >> n; a.resize(n); b.resize(n); G.resize(n); rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; rep(i,n-1){ int u,v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } rep(i,100010)rep(j,2) dp[i][j] = -LINF; cout << max(dfs(0,0,-1),dfs(0,1,-1)) << endl; }