結果

問題 No.1221 木 *= 3
ユーザー ゆきのんゆきのん
提出日時 2020-09-25 05:23:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 2,561 ms
コンパイル使用メモリ 167,768 KB
実行使用メモリ 21,292 KB
最終ジャッジ日時 2023-09-10 14:22:48
合計ジャッジ時間 7,609 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,620 KB
testcase_01 AC 4 ms
9,528 KB
testcase_02 AC 4 ms
9,472 KB
testcase_03 AC 4 ms
9,472 KB
testcase_04 AC 4 ms
9,732 KB
testcase_05 AC 4 ms
9,468 KB
testcase_06 AC 4 ms
9,728 KB
testcase_07 AC 164 ms
21,292 KB
testcase_08 AC 169 ms
20,936 KB
testcase_09 AC 169 ms
21,232 KB
testcase_10 AC 170 ms
21,028 KB
testcase_11 AC 172 ms
21,044 KB
testcase_12 AC 159 ms
13,416 KB
testcase_13 AC 149 ms
13,672 KB
testcase_14 AC 154 ms
13,384 KB
testcase_15 AC 154 ms
13,480 KB
testcase_16 AC 152 ms
13,432 KB
testcase_17 AC 173 ms
13,480 KB
testcase_18 AC 159 ms
13,488 KB
testcase_19 AC 168 ms
13,480 KB
testcase_20 AC 163 ms
13,480 KB
testcase_21 AC 169 ms
13,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=998244353;
const ll LINF=1ll<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

ll a[1<<17], b[1<<17];
ll dp[1<<17][2];
vector<int> G[1<<17];
int n;

ll dfs(int u, int v, bool f){
    if(dp[u][f] != -LINF) return dp[u][f];
    ll ret = 0;
    if(f){
        for(auto &g:G[u]){
            if(g == v) continue;
            ret += max(dfs(g, u, 1) + b[u] + b[g], dfs(g, u, 0));
        }
    }
    else{
        for(auto &g:G[u]){
            if(g == v) continue;
            ret += max(dfs(g, u, 1), dfs(g, u, 0));
        }
        ret += a[u];
    }
    return dp[u][f] = ret;
}




int main(){
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    for (int i = 0; i < n-1; i++) {
        int u,v;cin >> u >> v;
        u--,v--;
        G[u].pb(v);
        G[v].pb(u);
    }
    for (int i = 0; i < 1<<17; i++) {
        for (int j = 0; j < 2; j++) {
            dp[i][j] = -LINF;
        }
    }
    cout << max(dfs(0, 0, 0), dfs(0, 0, 1)) << endl;
    return 0;
}
0