結果

問題 No.2523 Trick Flower
ユーザー ShirotsumeShirotsume
提出日時 2023-10-25 12:33:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,676 ms / 4,000 ms
コード長 3,186 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 81,864 KB
実行使用メモリ 355,052 KB
最終ジャッジ日時 2023-10-25 12:33:39
合計ジャッジ時間 27,926 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
56,140 KB
testcase_01 AC 42 ms
56,140 KB
testcase_02 AC 42 ms
56,140 KB
testcase_03 AC 42 ms
56,140 KB
testcase_04 AC 45 ms
56,140 KB
testcase_05 AC 42 ms
56,140 KB
testcase_06 AC 42 ms
56,140 KB
testcase_07 AC 44 ms
58,188 KB
testcase_08 AC 138 ms
79,044 KB
testcase_09 AC 186 ms
80,028 KB
testcase_10 AC 45 ms
58,188 KB
testcase_11 AC 65 ms
73,160 KB
testcase_12 AC 82 ms
77,204 KB
testcase_13 AC 95 ms
77,520 KB
testcase_14 AC 75 ms
77,120 KB
testcase_15 AC 3,676 ms
354,068 KB
testcase_16 AC 3,379 ms
355,052 KB
testcase_17 AC 1,039 ms
274,216 KB
testcase_18 AC 328 ms
269,480 KB
testcase_19 AC 1,620 ms
236,580 KB
testcase_20 AC 1,572 ms
226,320 KB
testcase_21 AC 1,139 ms
217,420 KB
testcase_22 AC 1,259 ms
241,348 KB
testcase_23 AC 1,310 ms
228,440 KB
testcase_24 AC 1,121 ms
177,428 KB
testcase_25 AC 345 ms
256,532 KB
testcase_26 AC 2,151 ms
264,168 KB
testcase_27 AC 936 ms
264,348 KB
testcase_28 AC 1,328 ms
207,204 KB
testcase_29 AC 134 ms
114,256 KB
testcase_30 AC 976 ms
209,352 KB
testcase_31 AC 225 ms
82,772 KB
testcase_32 AC 415 ms
163,568 KB
testcase_33 AC 1,015 ms
254,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def scc(N,edges):
    M=len(edges)
    start=[0]*(N+1)
    elist=[0]*M
    for e in edges:
        start[e[0]+1]+=1
    for i in range(1,N+1):
        start[i]+=start[i-1]
    counter=start[:]
    for e in edges:
        elist[counter[e[0]]]=e[1]
        counter[e[0]]+=1
    visited=[]
    low=[0]*N
    Ord=[-1]*N
    ids=[0]*N
    NG=[0,0]
    def dfs(v):
        stack=[(v,-1,0),(v,-1,1)]
        while stack:
            v,bef,t=stack.pop()
            if t:
                if bef!=-1 and Ord[v]!=-1:
                    low[bef]=min(low[bef],Ord[v])
                    stack.pop()
                    continue
                low[v]=NG[0]
                Ord[v]=NG[0]
                NG[0]+=1
                visited.append(v)
                for i in range(start[v],start[v+1]):
                    to=elist[i]
                    if Ord[to]==-1:
                        stack.append((to,v,0))
                        stack.append((to,v,1))
                    else:
                        low[v]=min(low[v],Ord[to])
            else:
                if low[v]==Ord[v]:
                    while(True):
                        u=visited.pop()
                        Ord[u]=N
                        ids[u]=NG[1]
                        if u==v:
                            break
                    NG[1]+=1
                low[bef]=min(low[bef],low[v])
    for i in range(N):
        if Ord[i]==-1:
            dfs(i)
    for i in range(N):
        ids[i]=NG[1]-1-ids[i]
    group_num=NG[1]
    counts=[0]*group_num
    for x in ids:
        counts[x]+=1
    groups=[[] for i in range(group_num)]
    for i in range(N):
        groups[ids[i]].append(i)
    return groups

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353
sys.setrecursionlimit(10 ** 5 + 2)
n = ii()

a = li()

b = li()

c = li()
E = []
for i in range(n):
    E.append((c[i] - 1, i))
    
groups = scc(n, E)

leader = [0] * n
cnt = 0
for v in groups:
    for x in v:
        leader[x] = cnt
    cnt += 1
A = [0] * cnt
B = [0] * cnt

for i in range(n):
    A[leader[i]] += a[i]
    B[leader[i]] += b[i]
    
graph = [[] for _ in range(cnt)]
invgraph = [[] for _ in range(cnt)]
S = set()
for u, v in E:
    if leader[v] != leader[u]:
        S.add((leader[u], leader[v]))

for u, v in S:
    graph[u].append(v)

def f(x):
    dp = [0] * cnt
    r = [0] * cnt
    c = [0] * cnt
    visit = [False] * cnt
    def dfs(now):
        if visit[now]:
            return dp[now]
        visit[now] = True
        for to in graph[now]:
            r[now] += dfs(to)
            c[to] += 1
        dp[now] = min(0, A[now] - x * B[now] + r[now])
       
        return dp[now]
    
    
    for now in range(cnt):
        dfs(now)
        if c[now] == 0:
            if dp[now] == 0:
                pass
            else:
                return False
    return True
ok = 0
ng = 10 ** 15
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if f(mid):
        ok = mid
    else:
        ng = mid
        
print(ok)

        
0