結果

問題 No.2523 Trick Flower
ユーザー titiatitia
提出日時 2023-11-09 02:51:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,286 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 167,328 KB
最終ジャッジ日時 2023-11-09 02:51:54
合計ジャッジ時間 13,606 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 38 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 39 ms
53,460 KB
testcase_07 WA -
testcase_08 AC 59 ms
66,380 KB
testcase_09 AC 57 ms
66,404 KB
testcase_10 AC 40 ms
55,596 KB
testcase_11 AC 54 ms
64,312 KB
testcase_12 AC 49 ms
61,940 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1,324 ms
161,400 KB
testcase_16 AC 1,334 ms
160,204 KB
testcase_17 AC 242 ms
167,328 KB
testcase_18 AC 238 ms
164,212 KB
testcase_19 AC 616 ms
160,700 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 588 ms
163,644 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 339 ms
161,464 KB
testcase_26 WA -
testcase_27 AC 388 ms
158,600 KB
testcase_28 WA -
testcase_29 AC 115 ms
91,300 KB
testcase_30 AC 371 ms
144,112 KB
testcase_31 AC 91 ms
77,496 KB
testcase_32 AC 187 ms
107,204 KB
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
        
        

    
0