結果

問題 No.19 ステージの選択
ユーザー zundamo-tizundamo-ti
提出日時 2021-01-08 06:48:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 2,074 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-11-15 06:26:43
合計ジャッジ時間 2,250 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,752 KB
testcase_01 AC 31 ms
10,880 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 31 ms
10,880 KB
testcase_05 AC 31 ms
10,880 KB
testcase_06 AC 31 ms
10,880 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 31 ms
10,880 KB
testcase_09 AC 32 ms
10,880 KB
testcase_10 AC 30 ms
10,880 KB
testcase_11 AC 31 ms
10,880 KB
testcase_12 AC 31 ms
10,752 KB
testcase_13 AC 31 ms
10,880 KB
testcase_14 AC 31 ms
10,880 KB
testcase_15 AC 31 ms
10,752 KB
testcase_16 AC 31 ms
10,752 KB
testcase_17 AC 31 ms
10,752 KB
testcase_18 AC 32 ms
10,752 KB
testcase_19 AC 32 ms
10,880 KB
testcase_20 AC 31 ms
10,880 KB
testcase_21 AC 33 ms
10,880 KB
testcase_22 AC 31 ms
10,752 KB
testcase_23 AC 31 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**9)
readline = sys.stdin.readline

def dfs(x, G, visited, log):
    visited[x] = True
    for y in G[x]:
        if visited[y]:
            continue
        dfs(y, G, visited, log)
    log.append(x)

def dp(i, DAG, visited, diff_of_I, memo):
    visited[i] = True
    memo[i] = diff_of_I[i]
    sum_diff = 0
    for j in DAG[i]:
        sum_diff += diff_of_I[j]
        dp(j, DAG, visited, diff_of_I, memo)
        memo[i] += memo[j]
    memo[i] -= sum_diff / 2

def main():
    N = int(readline())
    fr = [[] for _ in range(N)]
    to = [[] for _ in range(N)]
    difficulty = [0]*N
    for x in range(N):
        d, y = map(int, readline().split())
        y -= 1
        fr[x].append(y)
        to[y].append(x)
        difficulty[x] = d

    visited = [False]*N
    log = []
    for x in range(N):
        if visited[x]:
            continue
        dfs(x, to, visited, log)
    log.reverse()

    visited = [False]*N
    groups = []
    for x in log:
        if visited[x]:
            continue
        group = []
        dfs(x, fr, visited, group)
        groups.append(group)

    I = len(groups)
    V_to_I = [-1]*N
    diff_of_I = [-1]*I
    for i, group in enumerate(groups):
        min_diff = 1000
        sum_diff = 0
        for x in group:
            V_to_I[x] = i
            min_diff = min(min_diff, difficulty[x])
            sum_diff += difficulty[x]
        diff_of_I[i] = (min_diff + sum_diff) / 2

    DAG = [[] for _ in range(I)]
    for x in range(N):
        i = V_to_I[x]
        for y in to[x]:
            j = V_to_I[y]
            if i != j:
                DAG[i].append(j)

    visited = [False]*I
    components = []
    for i in range(I):
        if visited[i]:
            continue
        component = []
        dfs(i, DAG, visited, component)
        components.append(component)

    ans = 0
    memo = [0]*I
    for component in components:
        top = component.pop()
        dp(top, DAG, visited, diff_of_I, memo)
        ans += memo[top]
    print(ans)

if __name__ == "__main__":
    main()

0