結果

問題 No.1221 木 *= 3
ユーザー carrot46carrot46
提出日時 2020-09-04 22:36:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 1,686 ms
コンパイル使用メモリ 172,108 KB
実行使用メモリ 22,096 KB
最終ジャッジ日時 2023-08-17 17:32:12
合計ジャッジ時間 5,458 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 175 ms
22,096 KB
testcase_08 AC 170 ms
21,800 KB
testcase_09 AC 171 ms
22,064 KB
testcase_10 AC 172 ms
21,692 KB
testcase_11 AC 173 ms
21,864 KB
testcase_12 AC 157 ms
12,488 KB
testcase_13 AC 153 ms
12,624 KB
testcase_14 AC 156 ms
12,532 KB
testcase_15 AC 158 ms
12,480 KB
testcase_16 AC 155 ms
12,580 KB
testcase_17 AC 179 ms
12,176 KB
testcase_18 AC 172 ms
12,248 KB
testcase_19 AC 172 ms
12,244 KB
testcase_20 AC 175 ms
12,432 KB
testcase_21 AC 171 ms
12,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

void dfs(int v, int p, mat &G, vec &a, vec &b, vec &r, vec &e){
    e[v] += a[v];
    for(auto to : G[v]){
        if(to != p){
            dfs(to, v, G, a, b, r, e);
            e[v] += max(r[to], e[to]);
            r[v] += max(b[v] + b[to] + r[to], e[to]);
        }
    }
}

int main() {
    cin>>N;
    mat G(N, vec(0));
    vec a(N), b(N), r(N, 0), e(N, 0);
    rep(i,N) cin>>a[i];
    rep(i,N) cin>>b[i];
    rep(i, N - 1){
        int u, t;
        cin>>u>>t;
        --u; --t;
        G[u].push_back(t);
        G[t].push_back(u);
    }
    dfs(0, -1, G, a, b, r, e);
    cout<<max(r[0], e[0])<<endl;
}
0