結果

問題 No.1221 木 *= 3
ユーザー tko919tko919
提出日時 2020-09-04 21:55:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 1,019 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 168,916 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-05-04 22:14:29
合計ジャッジ時間 4,701 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,888 KB
testcase_01 AC 2 ms
5,760 KB
testcase_02 AC 2 ms
5,888 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 2 ms
5,888 KB
testcase_05 AC 3 ms
5,888 KB
testcase_06 AC 2 ms
5,888 KB
testcase_07 AC 150 ms
19,016 KB
testcase_08 AC 148 ms
18,816 KB
testcase_09 AC 154 ms
19,072 KB
testcase_10 AC 149 ms
19,012 KB
testcase_11 AC 149 ms
18,988 KB
testcase_12 AC 144 ms
11,304 KB
testcase_13 AC 133 ms
11,304 KB
testcase_14 AC 143 ms
11,432 KB
testcase_15 AC 139 ms
11,432 KB
testcase_16 AC 137 ms
11,432 KB
testcase_17 AC 149 ms
11,336 KB
testcase_18 AC 142 ms
11,264 KB
testcase_19 AC 147 ms
11,392 KB
testcase_20 AC 146 ms
11,392 KB
testcase_21 AC 158 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

vector<int> g[101010];
int a[101010],b[101010];
ll dp[101010][2]={};
void dfs(int v,int p){
   ll sum0=0,sum1=0;
   for(int t:g[v])if(t!=p){
      dfs(t,v);
      sum1+=max(dp[t][1]+b[t]+b[v],dp[t][0]);
      sum0+=max(dp[t][0],dp[t][1]);
   }
   dp[v][1]=sum1;
   dp[v][0]=sum0+a[v];
}

int main(){
   int n; cin>>n;
   rep(i,0,n)cin>>a[i];
   rep(i,0,n)cin>>b[i];
   rep(i,0,n-1){
      int u,v; cin>>u>>v;
      u--; v--;
      g[u].push_back(v);
      g[v].push_back(u);
   }
   dfs(0,-1);
   ll res=max(dp[0][0],dp[0][1]);
   cout<<res<<endl;
   return 0;
}
0