結果
問題 |
No.1221 木 *= 3
|
ユーザー |
![]() |
提出日時 | 2020-09-06 11:32:31 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 199 ms / 2,000 ms |
コード長 | 1,494 bytes |
コンパイル時間 | 1,236 ms |
コンパイル使用メモリ | 121,632 KB |
最終ジャッジ日時 | 2025-01-14 07:36:20 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#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; }