結果

問題 No.2523 Trick Flower
ユーザー LyricalMaestro
提出日時 2025-03-10 00:27:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,575 ms / 4,000 ms
コード長 3,060 bytes
コンパイル時間 638 ms
コンパイル使用メモリ 82,628 KB
実行使用メモリ 148,352 KB
最終ジャッジ日時 2025-03-10 00:27:25
合計ジャッジ時間 14,870 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/2523

from collections import deque

def solve(in_degree, next_nodes, node_map, cycle_index_list, A, B, value):
    has_values = [0] * len(next_nodes)
    for i in range(len(A)):
        a = A[i]
        has_values[cycle_index_list[i]] += a

    need_values = [0] * len(next_nodes)
    for i in range(len(B)):
        b = B[i] * value
        need_values[cycle_index_list[i]] += b

    queue = deque()
    for i in range(len(next_nodes)):
        if in_degree[i] == 0:
            queue.append(i)
        
    while len(queue) > 0:
        i = queue.popleft()
        required_value = max(0, need_values[i] - has_values[i])
        if len(next_nodes[i]) == 0 and required_value > 0:
            return False

        for j in next_nodes[i]:
            need_values[j] += required_value
            in_degree[j] -= 1
            if in_degree[j] == 0:
                queue.append(j)
    return True


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    # 「強連結成分」を1成分としたDAGを構築
    cycle_index_list = [-2] * N
    cycle_index = 0
    for s_i in range(N):
        if cycle_index_list[s_i] == -2:
            s_map = {}
            s = s_i
            while s not in s_map and cycle_index_list[s] == -2:
                s_map[s] = True
                cycle_index_list[s] = -1
                s = C[s] - 1

            if s not in s_map and cycle_index_list[s] != -2:
                continue
            
            array = [s]
            v = C[s] - 1
            while v != s:
                array.append(v)
                v = C[v] - 1
            for s in array:
                cycle_index_list[s] = cycle_index
            cycle_index += 1
    
    node_map = {}
    composition_index = cycle_index
    for i in range(N):
        if cycle_index_list[i] == -1:
            node_map[composition_index] = [i]
            cycle_index_list[i] = composition_index
            composition_index += 1
        else:
            c = cycle_index_list[i]
            if c not in node_map:
                node_map[c] = []
            node_map[c].append(i)

    next_nodes = [[] for _ in range(composition_index)]
    in_degree = [0] * composition_index
    for i in range(N):
        c_f = cycle_index_list[i]
        c_t = cycle_index_list[C[i] - 1]
        if c_f == c_t:
            continue
        else:
            next_nodes[c_f].append(c_t)
            in_degree[c_t] += 1

    # 2分探索
    a_total = sum(A)
    b_total = sum(B)
    max_n = a_total // b_total

    low = 0
    high = max_n
    while high - low > 1:
        mid = (high + low) // 2
        if solve(in_degree.copy(), next_nodes, node_map, cycle_index_list, A, B, mid):
            low = mid
        else:
            high = mid
    if solve(in_degree.copy(), next_nodes, node_map, cycle_index_list, A, B, high):
        print(high)
    else:
        print(low)










    



if __name__ == "__main__":
    main()
0