結果

問題 No.1221 木 *= 3
ユーザー 沙耶花沙耶花
提出日時 2020-09-04 22:25:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 961 bytes
コンパイル時間 2,395 ms
コンパイル使用メモリ 214,420 KB
実行使用メモリ 23,380 KB
最終ジャッジ日時 2023-08-17 17:04:41
合計ジャッジ時間 5,792 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 98 ms
23,376 KB
testcase_08 AC 102 ms
23,264 KB
testcase_09 AC 99 ms
23,300 KB
testcase_10 AC 100 ms
23,188 KB
testcase_11 AC 98 ms
23,380 KB
testcase_12 AC 85 ms
16,132 KB
testcase_13 AC 83 ms
16,068 KB
testcase_14 AC 85 ms
16,128 KB
testcase_15 AC 84 ms
16,036 KB
testcase_16 AC 85 ms
16,024 KB
testcase_17 AC 101 ms
15,788 KB
testcase_18 AC 100 ms
15,764 KB
testcase_19 AC 100 ms
15,904 KB
testcase_20 AC 102 ms
15,788 KB
testcase_21 AC 100 ms
15,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000000	

int N;
vector<vector<int>> E;
vector<long long> a,b;

vector<vector<long long>> dp;

void dfs(int now,int p){
	dp[now][0] = a[now];
	dp[now][1] = 0LL;
	for(int i=0;i<E[now].size();i++){
		int to = E[now][i];
		if(to==p)continue;
		dfs(to,now);
		dp[now][0] += max(dp[to][0],dp[to][1]);
		dp[now][1] += max(dp[to][0],dp[to][1] + b[to] + b[now]);
	}
	
}

int main(){

	cin>>N;
	E.resize(N,vector<int>());
	a.resize(N);
	b.resize(N);
	
	rep(i,N)scanf("%lld",&a[i]);
	rep(i,N)scanf("%lld",&b[i]);
	rep(i,N-1){
		int x,y;
		scanf("%d %d",&x,&y);
		x--;y--;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	
	dp.resize(N,vector<long long>(2,0LL));
	
	dfs(0,-1);
	//rep(i,N)cout<<dp[i][0]<<','<<dp[i][1]<<endl;
	cout<<max(dp[0][0],dp[0][1])<<endl;
	
	return 0;
}
0