結果

問題 No.1221 木 *= 3
ユーザー DriceDrice
提出日時 2020-09-04 22:45:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 929 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 48,980 KB
実行使用メモリ 15,932 KB
最終ジャッジ日時 2023-08-17 20:25:53
合計ジャッジ時間 3,023 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,328 KB
testcase_01 AC 3 ms
5,384 KB
testcase_02 AC 3 ms
5,372 KB
testcase_03 AC 2 ms
5,316 KB
testcase_04 AC 2 ms
5,328 KB
testcase_05 AC 3 ms
5,384 KB
testcase_06 AC 3 ms
5,392 KB
testcase_07 AC 85 ms
15,932 KB
testcase_08 AC 82 ms
15,764 KB
testcase_09 AC 82 ms
15,908 KB
testcase_10 AC 85 ms
15,744 KB
testcase_11 AC 84 ms
15,796 KB
testcase_12 AC 69 ms
12,012 KB
testcase_13 AC 69 ms
12,020 KB
testcase_14 AC 72 ms
12,024 KB
testcase_15 AC 70 ms
11,952 KB
testcase_16 AC 70 ms
12,040 KB
testcase_17 AC 86 ms
12,048 KB
testcase_18 AC 85 ms
12,116 KB
testcase_19 AC 87 ms
12,048 KB
testcase_20 AC 83 ms
12,108 KB
testcase_21 AC 82 ms
12,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<vector>
long long a[100005],b[100005];
std::vector<int>g[100005];
//0: delete, 1: have
long long f[100005][2];
int vis[100005];

long long max(long long a,long long b){ return a>b?a:b; }

void dp(int u){
    vis[u] = 1;
    f[u][0] = f[u][1] = 0;
    for(int v: g[u]){
        if(!vis[v]){
            dp(v);
            f[u][0] = f[u][0]+max(f[v][0],f[v][1]);
            f[u][1] = max(f[u][1]+f[v][0],f[u][1]+b[u]+b[v]+f[v][1]);
        }
    }
    f[u][0] += a[u];
    //printf("f[%d][0] = %lld, f[%d][1] = %lld\n",u,f[u][0],u,f[u][1]);
}

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) scanf("%lld",&a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld",&b[i]);
    for(int i = 1; i <= n-1; i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dp(1);
    printf("%lld\n",max(f[1][1],f[1][0]));
    return 0;
}
0