結果

問題 No.2523 Trick Flower
ユーザー chineristACchineristAC
提出日時 2023-10-27 21:40:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,364 ms / 4,000 ms
コード長 1,694 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 82,572 KB
実行使用メモリ 261,348 KB
最終ジャッジ日時 2024-09-25 13:45:50
合計ジャッジ時間 20,106 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
57,608 KB
testcase_01 AC 45 ms
56,656 KB
testcase_02 AC 45 ms
57,900 KB
testcase_03 AC 45 ms
56,440 KB
testcase_04 AC 45 ms
56,864 KB
testcase_05 AC 46 ms
56,836 KB
testcase_06 AC 45 ms
56,756 KB
testcase_07 AC 53 ms
65,420 KB
testcase_08 AC 82 ms
77,104 KB
testcase_09 AC 82 ms
77,040 KB
testcase_10 AC 59 ms
68,356 KB
testcase_11 AC 67 ms
72,280 KB
testcase_12 AC 61 ms
69,620 KB
testcase_13 AC 81 ms
77,424 KB
testcase_14 AC 81 ms
77,040 KB
testcase_15 AC 581 ms
144,668 KB
testcase_16 AC 590 ms
144,220 KB
testcase_17 AC 799 ms
261,348 KB
testcase_18 AC 626 ms
229,028 KB
testcase_19 AC 1,211 ms
143,916 KB
testcase_20 AC 1,352 ms
143,908 KB
testcase_21 AC 1,335 ms
143,760 KB
testcase_22 AC 1,185 ms
143,732 KB
testcase_23 AC 1,300 ms
143,752 KB
testcase_24 AC 675 ms
113,356 KB
testcase_25 AC 1,009 ms
133,356 KB
testcase_26 AC 1,008 ms
134,552 KB
testcase_27 AC 1,287 ms
141,980 KB
testcase_28 AC 925 ms
122,696 KB
testcase_29 AC 235 ms
84,988 KB
testcase_30 AC 1,004 ms
117,464 KB
testcase_31 AC 142 ms
79,184 KB
testcase_32 AC 576 ms
100,432 KB
testcase_33 AC 1,364 ms
144,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
import heapq
from collections import deque
import random

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N = int(input())
A = li()
B = li()
C = li()

C = [c-1 for c in C]

cycle = [C[i] for i in range(N)]
for _ in range(30):
    cycle = [cycle[cycle[i]] for i in range(N)]
cycle = set(cycle)

comp_cycle = [i for i in range(N)]
for i in range(N):
    if i not in cycle or comp_cycle[i]!=i:
        continue

    t = len(comp_cycle)
    tmp = [i]
    while True:
        nxt = C[tmp[-1]]
        if nxt!=i:
            tmp.append(nxt)
        else:
            break
    for v in tmp:
        comp_cycle[v] = t
    comp_cycle.append(t)
    A.append(sum([A[i] for i in tmp]))
    B.append(sum([B[i] for i in tmp]))

n = len(comp_cycle)
edge = [[] for v in range(n)]
parent = [-1] * n
for i in range(N):
    if comp_cycle[i]!=i:
        continue
    parent[comp_cycle[i]] = comp_cycle[C[i]]
    edge[comp_cycle[C[i]]].append(comp_cycle[i])

topo = []
for root in range(N,n):
    deq = deque([root])
    while deq:
        v = deq.popleft()
        topo.append(v)
        for nv in edge[v]:
            deq.append(nv)

dp = [-1] * n
def cond(k):
    for v in topo[::-1]:
        dp[v] = k * B[v]
        for nv in edge[v]:
            dp[v] += dp[nv]
        if dp[v] > A[v]:
            dp[v] -= A[v]
        else:
            dp[v] = 0
    
    for root in range(N,n):
        if dp[root]:
            return False
    return True

ok = 0
ng = 10**15 + 1
while ng-ok>1:
    mid = (ok+ng)//2
    if cond(mid):
        ok = mid
    else:
        ng = mid

print(ok)
        
        
    

0