結果

問題 No.1221 木 *= 3
ユーザー carrot46carrot46
提出日時 2020-09-04 22:36:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 175,148 KB
実行使用メモリ 22,272 KB
最終ジャッジ日時 2024-05-05 00:13:38
合計ジャッジ時間 5,659 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 186 ms
22,272 KB
testcase_08 AC 184 ms
21,888 KB
testcase_09 AC 183 ms
22,272 KB
testcase_10 AC 186 ms
22,004 KB
testcase_11 AC 186 ms
22,000 KB
testcase_12 AC 171 ms
12,600 KB
testcase_13 AC 165 ms
12,600 KB
testcase_14 AC 168 ms
12,588 KB
testcase_15 AC 167 ms
12,600 KB
testcase_16 AC 168 ms
12,584 KB
testcase_17 AC 184 ms
12,372 KB
testcase_18 AC 184 ms
12,416 KB
testcase_19 AC 186 ms
12,416 KB
testcase_20 AC 188 ms
12,544 KB
testcase_21 AC 188 ms
12,416 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