結果

問題 No.1221 木 *= 3
ユーザー titiatitia
提出日時 2020-09-05 05:17:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 704 ms / 2,000 ms
コード長 981 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 11,064 KB
実行使用メモリ 45,820 KB
最終ジャッジ日時 2023-08-18 05:29:32
合計ジャッジ時間 11,894 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,864 KB
testcase_01 AC 18 ms
8,288 KB
testcase_02 AC 17 ms
8,276 KB
testcase_03 AC 16 ms
7,796 KB
testcase_04 AC 17 ms
7,956 KB
testcase_05 AC 16 ms
8,340 KB
testcase_06 AC 16 ms
7,864 KB
testcase_07 AC 662 ms
44,520 KB
testcase_08 AC 668 ms
45,820 KB
testcase_09 AC 677 ms
45,180 KB
testcase_10 AC 662 ms
45,184 KB
testcase_11 AC 663 ms
45,144 KB
testcase_12 AC 605 ms
38,060 KB
testcase_13 AC 597 ms
38,196 KB
testcase_14 AC 619 ms
38,052 KB
testcase_15 AC 613 ms
38,140 KB
testcase_16 AC 600 ms
38,036 KB
testcase_17 AC 690 ms
40,540 KB
testcase_18 AC 668 ms
41,156 KB
testcase_19 AC 702 ms
40,428 KB
testcase_20 AC 701 ms
40,380 KB
testcase_21 AC 704 ms
40,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
A=[0]+list(map(int,input().split()))
B=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]

for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

QUE=[1] 
Parent=[-1]*(N+1)
Parent[1]=N+1
TOP_SORT=[] # トポロジカルソート

while QUE:
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            QUE.append(to)

DP0=[-1<<100]*(N+1)
DP1=[-1<<100]*(N+1)

for x in TOP_SORT[::-1]:
    if x!=1 and len(E[x])==1:
        DP0[x]=A[x]
        DP1[x]=0

    else:
        S=0
        for to in E[x]:
            if to==Parent[x]:
                continue
            S+=max(DP0[to],DP1[to])

        DP0[x]=S+A[x]

        S2=0
        for to in E[x]:
            if to==Parent[x]:
                continue
            S2+=max(DP1[to]+B[x]+B[to],DP0[to])

        DP1[x]=S2

print(max(DP0[1],DP1[1]))
        
    


0