結果
問題 | No.1221 木 *= 3 |
ユーザー | fura |
提出日時 | 2020-09-13 19:15:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 111 ms / 2,000 ms |
コード長 | 1,107 bytes |
コンパイル時間 | 2,064 ms |
コンパイル使用メモリ | 203,692 KB |
実行使用メモリ | 21,708 KB |
最終ジャッジ日時 | 2023-09-03 23:51:27 |
合計ジャッジ時間 | 6,963 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,588 KB |
testcase_01 | AC | 2 ms
5,808 KB |
testcase_02 | AC | 2 ms
5,644 KB |
testcase_03 | AC | 2 ms
5,596 KB |
testcase_04 | AC | 2 ms
5,584 KB |
testcase_05 | AC | 2 ms
5,668 KB |
testcase_06 | AC | 2 ms
5,536 KB |
testcase_07 | AC | 104 ms
21,512 KB |
testcase_08 | AC | 109 ms
21,308 KB |
testcase_09 | AC | 108 ms
21,708 KB |
testcase_10 | AC | 110 ms
21,132 KB |
testcase_11 | AC | 111 ms
21,312 KB |
testcase_12 | AC | 79 ms
14,128 KB |
testcase_13 | AC | 83 ms
14,084 KB |
testcase_14 | AC | 84 ms
14,088 KB |
testcase_15 | AC | 82 ms
14,024 KB |
testcase_16 | AC | 82 ms
14,028 KB |
testcase_17 | AC | 98 ms
13,788 KB |
testcase_18 | AC | 97 ms
13,792 KB |
testcase_19 | AC | 99 ms
13,808 KB |
testcase_20 | AC | 95 ms
13,832 KB |
testcase_21 | AC | 100 ms
13,808 KB |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; using graph=vector<vector<int>>; void add_undirected_edge(graph& G,int u,int v){ G[u].emplace_back(v); G[v].emplace_back(u); } graph T; lint a[100000],b[100000]; bool vis[100000][2][2]; lint memo[100000][2][2]; // bu : u が消されずに残っているかどうか // bp : p が消されずに残っているかどうか lint dfs(int u,int p,bool bu,bool bp){ lint& res=memo[u][bu][bp]; if(vis[u][bu][bp]) return res; vis[u][bu][bp]=true; if(!bu){ res=a[u]; for(int v:T[u]) if(v!=p) { res+=max(dfs(v,u,false,false),dfs(v,u,true,false)); } return res; } else{ res=(bp?b[u]:0); for(int v:T[u]) if(v!=p) { res+=max(dfs(v,u,false,true),dfs(v,u,true,true)+b[u]); } } return res; } int main(){ int n; scanf("%d",&n); rep(u,n) scanf("%lld",&a[u]); rep(u,n) scanf("%lld",&b[u]); T.resize(n); rep(i,n-1){ int u,v; scanf("%d%d",&u,&v); u--; v--; add_undirected_edge(T,u,v); } printf("%lld\n",max(dfs(0,-1,false,false),dfs(0,-1,true,false))); return 0; }