結果

問題 No.1221 木 *= 3
ユーザー takakintakakin
提出日時 2020-09-09 22:37:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,075 ms / 2,000 ms
コード長 993 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 10,796 KB
実行使用メモリ 53,424 KB
最終ジャッジ日時 2023-08-22 13:23:19
合計ジャッジ時間 16,498 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,764 KB
testcase_01 AC 19 ms
8,692 KB
testcase_02 AC 18 ms
8,776 KB
testcase_03 AC 18 ms
8,696 KB
testcase_04 AC 18 ms
8,768 KB
testcase_05 AC 18 ms
8,828 KB
testcase_06 AC 18 ms
8,656 KB
testcase_07 AC 915 ms
51,996 KB
testcase_08 AC 915 ms
53,424 KB
testcase_09 AC 940 ms
52,572 KB
testcase_10 AC 916 ms
52,608 KB
testcase_11 AC 933 ms
52,700 KB
testcase_12 AC 860 ms
46,484 KB
testcase_13 AC 858 ms
46,756 KB
testcase_14 AC 859 ms
46,272 KB
testcase_15 AC 858 ms
46,316 KB
testcase_16 AC 881 ms
46,228 KB
testcase_17 AC 1,075 ms
47,880 KB
testcase_18 AC 1,058 ms
48,620 KB
testcase_19 AC 1,047 ms
47,952 KB
testcase_20 AC 1,049 ms
47,940 KB
testcase_21 AC 1,065 ms
47,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

edge=[[] for i in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)
inf=float("inf")
Par=[inf]*n
Par[0]=-1
Deg=[0]*n
Deg[0]=0
Chk=[0]
while Chk:
  c=Chk.pop()
  for next in edge[c]:
    if Par[next]==inf:
      Par[next]=c
      Deg[next]+=1
      Chk.append(next)
from collections import deque
TS=list(v for v in range(n) if Deg[v]==0)
D=deque(TS)
while D:
  v=D.popleft()
  for t in edge[v]:
    if t!=Par[v]:
      Deg[t]-=1
      if Deg[t]==0:
        D.append(t)
        TS.append(t)
P=[[0]*2 for _ in range(n)]
for i in range(n)[::-1]:
	cur=TS[i]
	Chd=[]
	for c in edge[cur]:
		if c!=Par[cur]:
			Chd.append(c)
	if not Chd:
		P[cur][0]=A[cur]
	else:
		for c in Chd:
			P[cur][0]+=max(P[c][0],P[c][1])
			P[cur][1]+=max(P[c][0],P[c][1]+B[cur]+B[c])
		P[cur][0]+=A[cur]
print(max(P[0]))
0