結果

問題 No.19 ステージの選択
ユーザー zundamo-tizundamo-ti
提出日時 2021-01-08 06:52:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 1,788 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-11-15 06:36:29
合計ジャッジ時間 1,793 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 28 ms
10,752 KB
testcase_04 AC 28 ms
10,880 KB
testcase_05 AC 28 ms
10,880 KB
testcase_06 AC 28 ms
10,880 KB
testcase_07 AC 29 ms
10,880 KB
testcase_08 AC 29 ms
10,880 KB
testcase_09 AC 30 ms
10,880 KB
testcase_10 AC 29 ms
10,880 KB
testcase_11 AC 29 ms
10,880 KB
testcase_12 AC 28 ms
10,880 KB
testcase_13 AC 29 ms
10,880 KB
testcase_14 AC 30 ms
10,880 KB
testcase_15 AC 29 ms
10,880 KB
testcase_16 AC 29 ms
10,880 KB
testcase_17 AC 30 ms
10,880 KB
testcase_18 AC 28 ms
11,008 KB
testcase_19 AC 30 ms
10,880 KB
testcase_20 AC 29 ms
10,752 KB
testcase_21 AC 27 ms
10,880 KB
testcase_22 AC 28 ms
10,752 KB
testcase_23 AC 28 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 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)

    ans = 0
    visited = [False]*I
    for i in range(I):
        if visited[i]:
            continue
        component = []
        dfs(i, DAG, visited, component)
        component.reverse()
        for i, c in enumerate(component):
            if i == 0:
                ans += diff_of_I[c]
            else:
                ans += diff_of_I[c] / 2
    print(ans)

if __name__ == "__main__":
    main()

0