結果

問題 No.2523 Trick Flower
ユーザー MasKoaTSMasKoaTS
提出日時 2023-10-27 15:25:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 846 ms / 4,000 ms
コード長 3,580 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 81,832 KB
実行使用メモリ 267,620 KB
最終ジャッジ日時 2023-10-27 15:25:33
合計ジャッジ時間 15,879 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
85,420 KB
testcase_01 AC 101 ms
85,420 KB
testcase_02 AC 122 ms
85,424 KB
testcase_03 AC 104 ms
85,420 KB
testcase_04 AC 105 ms
85,424 KB
testcase_05 AC 115 ms
85,424 KB
testcase_06 AC 121 ms
85,424 KB
testcase_07 AC 111 ms
88,424 KB
testcase_08 AC 128 ms
88,924 KB
testcase_09 AC 130 ms
88,792 KB
testcase_10 AC 130 ms
88,460 KB
testcase_11 AC 141 ms
88,620 KB
testcase_12 AC 119 ms
88,476 KB
testcase_13 AC 127 ms
88,776 KB
testcase_14 AC 125 ms
88,752 KB
testcase_15 AC 606 ms
261,608 KB
testcase_16 AC 660 ms
262,092 KB
testcase_17 AC 846 ms
267,620 KB
testcase_18 AC 665 ms
263,160 KB
testcase_19 AC 680 ms
251,808 KB
testcase_20 AC 626 ms
243,320 KB
testcase_21 AC 644 ms
224,244 KB
testcase_22 AC 617 ms
256,688 KB
testcase_23 AC 654 ms
260,720 KB
testcase_24 AC 446 ms
152,592 KB
testcase_25 AC 583 ms
247,240 KB
testcase_26 AC 559 ms
237,432 KB
testcase_27 AC 596 ms
261,128 KB
testcase_28 AC 535 ms
226,128 KB
testcase_29 AC 221 ms
104,836 KB
testcase_30 AC 488 ms
176,332 KB
testcase_31 AC 177 ms
91,728 KB
testcase_32 AC 380 ms
139,476 KB
testcase_33 AC 627 ms
207,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools as iter
import collections as coll
import heapq as hq
import bisect as bis
from decimal import Decimal as dec
from functools import cmp_to_key
import math
import sys
#import pypyjit
#pypyjit.set_param('max_unroll_recursion=-1')
sys.setrecursionlimit(10 ** 6)
inp = sys.stdin.readline
input = lambda : inp()[:-1]
getN = lambda : int(inp())
getNs = lambda : map(int, inp().split())
getList = lambda :list(map(int, inp().split()))
getStrs = lambda n : [input() for _ in [0] * n]
getEdges = lambda n : [[x - 1 for x in getNs()] for _ in [0] * n]
def yexit(): print("Yes"); exit(0)
def nexit(): print("No"); exit(0)
pi = 3.141592653589793
mod = 1000000007
MOD = 998244353
INF = 4611686018427387903
dx = [1, 0, -1, 0];  dy = [0, 1, 0, -1]
#di = coll.defaultdict(int)

"""
強連結成分分解(SCC)
ref : https://manabitimes.jp/math/1250
"""

class SCC:
    def __init__(self, n):
        self.n = n
        self.route = [[] for _ in [0] * n]
        self.rev_route = [[] for _ in [0] * n]
        self.label = [-1] * n
        self.count = 0
    
    def add_edge(self, v_first, v_second):
        self.route[v_first].append(v_second)
        self.rev_route[v_second].append(v_first)

    def _build(self):
        self.v_list = []
        self.visited = set([])
        for v in range(self.n):
            if(v in self.visited):
                continue
            self._dfs(v)
        for v in reversed(self.v_list):
            if(self.label[v] != -1):
                continue
            self._rev_dfs(v)
            self.count += 1

    def _dfs(self, v_start):
        stack = [v_start, 0]
        while(stack):
            v, id = stack[-2:]
            if(id == 0 and v in self.visited):
                stack.pop()
                stack.pop()
                continue
            self.visited.add(v)
            if(id < len(self.route[v])):
                stack[-1] += 1
                stack.extend([self.route[v][id], 0])
                continue
            stack.pop()
            self.v_list.append(stack.pop())

    def _rev_dfs(self, v_start):
        stack = [v_start]
        self.label[v_start] = self.count
        while(stack):
            v_now = stack.pop()
            for v_next in self.rev_route[v_now]:
                if(self.label[v_next] != -1):
                    continue
                self.label[v_next] = self.count
                stack.append(v_next)

    def construct(self):
        self._build()
        dag = [[] for _ in [0] * self.count]
        group = [[] for _ in [0] * self.count]
        for v, lb in enumerate(self.label):
            for v_next in self.route[v]:
                lb_next = self.label[v_next]
                if(lb == lb_next):
                    continue
                dag[lb].append(lb_next)
            group[lb].append(v)
        return dag, group

 
"""
Main Code
"""

N = getN()
A, B, C = [getList() for _ in [0] * 3]

scc = SCC(N)
for i, x in enumerate(C):
    scc.add_edge(x - 1, i)
dag, groups = scc.construct()

n = len(groups)
a = [sum(A[x] for x in group) for group in groups]
b = [sum(B[x] for x in group) for group in groups]
rtree = [-1] * n
for x, ys in enumerate(dag):
    for y in ys:
        rtree[y] = x

def check(mid):
    dp = [x - mid * y for x, y in zip(a, b)]
    for x in range(n - 1, -1, -1):
        if(rtree[x] == -1 and dp[x] < 0):
            return False
        dp[rtree[x]] += min(dp[x], 0)
    return True

ok = 0
ng = 10 ** 14 + 1
while(abs(ok - ng) > 1):
    mid = (ok + ng) >> 1
    if check(mid):
        ok = mid
    else:
        ng = mid
print(ok)
0