結果

問題 No.1221 木 *= 3
ユーザー DriceDrice
提出日時 2020-09-04 22:45:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 929 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 49,280 KB
実行使用メモリ 17,280 KB
最終ジャッジ日時 2024-05-05 03:20:20
合計ジャッジ時間 2,416 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,504 KB
testcase_01 AC 2 ms
5,504 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,632 KB
testcase_06 AC 3 ms
5,504 KB
testcase_07 AC 72 ms
17,280 KB
testcase_08 AC 72 ms
17,152 KB
testcase_09 AC 71 ms
17,280 KB
testcase_10 AC 70 ms
17,152 KB
testcase_11 AC 79 ms
17,152 KB
testcase_12 AC 66 ms
12,304 KB
testcase_13 AC 62 ms
12,180 KB
testcase_14 AC 63 ms
12,256 KB
testcase_15 AC 63 ms
12,180 KB
testcase_16 AC 62 ms
12,300 KB
testcase_17 AC 67 ms
12,288 KB
testcase_18 AC 68 ms
12,288 KB
testcase_19 AC 72 ms
12,288 KB
testcase_20 AC 74 ms
12,160 KB
testcase_21 AC 69 ms
12,288 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