結果
| 問題 |
No.2523 Trick Flower
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2023-11-09 02:51:39 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,286 bytes |
| コンパイル時間 | 291 ms |
| コンパイル使用メモリ | 82,320 KB |
| 実行使用メモリ | 167,676 KB |
| 最終ジャッジ日時 | 2024-09-25 23:59:53 |
| 合計ジャッジ時間 | 12,465 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 WA * 10 |
ソースコード
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
for i in range(N):
C[i]-=1
# (一般グラフの)トポロジカルソート、SCC
E=[[] for i in range(N)]
E_INV=[[] for i in range(N)]
for i in range(N):
E[C[i]].append(i)
E_INV[i].append(C[i])
# DFSして帰り際にTOPに点を放り込んでいる。
# NOWで現在地点、USEINDで、どこの辺まで既に見たか、を調べている。
def Top_sort(E):
Parent=[-1]*N
USEIND=[0]*N
TOP=[]
for ROOT in range(N):
if Parent[ROOT]!=-1:
continue
Parent[ROOT]=ROOT
NOW=ROOT
while NOW!=ROOT or USEIND[ROOT]!=len(E[ROOT]):
if USEIND[NOW]==len(E[NOW]):
TOP.append(NOW)
NOW=Parent[NOW]
elif E[NOW][USEIND[NOW]]==Parent[NOW]:
USEIND[NOW]+=1
else:
NEXT=E[NOW][USEIND[NOW]]
USEIND[NOW]+=1
if Parent[NEXT]==-1:
Parent[NEXT]=NOW
NOW=NEXT
TOP.append(ROOT)
return TOP[::-1]
USE=[0]*N
SCC=[]
# SCCを調べるための逆順DFS。
# やっていることはhttps://manabitimes.jp/math/1250 などと同じ。
def dfs2(x):
Q=[x]
USE[x]=1
ANS=[]
while Q:
x=Q.pop()
ANS.append(x)
for to in E_INV[x]:
if USE[to]==0:
USE[to]=1
Q.append(to)
return ANS
TOP_SORT=Top_sort(E)
for x in TOP_SORT:
if USE[x]==0:
SCC.append(dfs2(x))
REP=[-1]*N
for scc in SCC:
rep=scc[0]
for s in scc:
REP[s]=rep
E=[[] for i in range(N)]
E_INV=[[] for i in range(N)]
for i in range(N):
E[REP[C[i]]].append(REP[i])
E_INV[REP[i]].append(REP[C[i]])
OK=0
NG=10**15
while NG>OK+1:
mid=(OK+NG)//2
rest=0
flag=1
DP=[0]*N
for i in range(len(SCC)):
need=0
for sc in SCC[i]:
need+=B[sc]*mid-A[sc]
for fr in E_INV[SCC[i][0]]:
need-=DP[fr]
if need>0:
flag=0
break
else:
DP[SCC[i][0]]=-need
if flag==1:
OK=mid
else:
NG=mid
print(OK)
titia