結果
問題 | No.1221 木 *= 3 |
ユーザー |
![]() |
提出日時 | 2020-09-04 22:44:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 224 ms / 2,000 ms |
コード長 | 1,122 bytes |
コンパイル時間 | 1,990 ms |
コンパイル使用メモリ | 200,660 KB |
最終ジャッジ日時 | 2025-01-14 06:08:12 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h> #define rep(a,n) for (int a = 0; a < (n); ++a) using namespace std; using ll = long long; typedef pair<int,int> P; typedef pair<ll,P> PP; typedef vector<vector<int> > Graph; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e18; ll dp[100010][2]; Graph g; vector<ll>a,b; void dfs(int v,int p=-1){ ll res1 = 0; ll res0 = 0; for(int i=0;i<g[v].size();i++){ int x = g[v][i]; if(x==p)continue; dfs(x,v); res1 += max(dp[x][0],dp[x][1]+b[x]+b[v]); res0 += max(dp[x][0],dp[x][1]); } res0 += a[v]; dp[v][0]=res0; dp[v][1]=res1; return; } int main(){ ll n; cin >> n; a.resize(n); b.resize(n); rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; g.resize(n); rep(i,n-1){ ll u,v; cin >> u >> v; u--;v--; g[u].push_back(v); g[v].push_back(u); } dfs(0); cout << max(dp[0][0],dp[0][1]) << endl; return 0; }