結果
問題 | No.1221 木 *= 3 |
ユーザー |
![]() |
提出日時 | 2020-09-09 22:37:20 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,350 ms / 2,000 ms |
コード長 | 993 bytes |
コンパイル時間 | 269 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 55,860 KB |
最終ジャッジ日時 | 2024-12-16 10:43:44 |
合計ジャッジ時間 | 19,063 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
import sysinput=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]*nPar[0]=-1Deg=[0]*nDeg[0]=0Chk=[0]while Chk:c=Chk.pop()for next in edge[c]:if Par[next]==inf:Par[next]=cDeg[next]+=1Chk.append(next)from collections import dequeTS=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]-=1if 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]))