結果
問題 | No.1221 木 *= 3 |
ユーザー |
|
提出日時 | 2020-09-04 22:45:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 93 ms / 2,000 ms |
コード長 | 929 bytes |
コンパイル時間 | 418 ms |
コンパイル使用メモリ | 49,152 KB |
実行使用メモリ | 17,408 KB |
最終ジャッジ日時 | 2024-11-26 20:39:48 |
合計ジャッジ時間 | 2,755 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#include<cstdio>#include<vector>long long a[100005],b[100005];std::vector<int>g[100005];//0: delete, 1: havelong long f[100005][2];int vis[100005];long long max(long long a,long long b){ return a>b?a:b; }void dp(int u){vis[u] = 1;f[u][0] = f[u][1] = 0;for(int v: g[u]){if(!vis[v]){dp(v);f[u][0] = f[u][0]+max(f[v][0],f[v][1]);f[u][1] = max(f[u][1]+f[v][0],f[u][1]+b[u]+b[v]+f[v][1]);}}f[u][0] += a[u];//printf("f[%d][0] = %lld, f[%d][1] = %lld\n",u,f[u][0],u,f[u][1]);}int main(){int n;scanf("%d",&n);for(int i = 1; i <= n; i++) scanf("%lld",&a[i]);for(int i = 1; i <= n; i++) scanf("%lld",&b[i]);for(int i = 1; i <= n-1; i++){int u,v;scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);}dp(1);printf("%lld\n",max(f[1][1],f[1][0]));return 0;}