結果
| 問題 |
No.1221 木 *= 3
|
| ユーザー |
|
| 提出日時 | 2020-09-13 19:15:34 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 121 ms / 2,000 ms |
| コード長 | 1,107 bytes |
| コンパイル時間 | 2,297 ms |
| コンパイル使用メモリ | 197,872 KB |
| 最終ジャッジ日時 | 2025-01-14 14:33:00 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:45:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
45 | int n; scanf("%d",&n);
| ~~~~~^~~~~~~~~
main.cpp:46:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
46 | rep(u,n) scanf("%lld",&a[u]);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:47:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
47 | rep(u,n) scanf("%lld",&b[u]);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:50:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
50 | int u,v; scanf("%d%d",&u,&v); u--; v--;
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#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;
}