結果

問題 No.19 ステージの選択
ユーザー H3PO4H3PO4
提出日時 2021-02-08 09:52:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,136 bytes
コンパイル時間 70 ms
コンパイル使用メモリ 11,048 KB
実行使用メモリ 8,892 KB
最終ジャッジ日時 2023-09-18 15:37:56
合計ジャッジ時間 1,868 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,796 KB
testcase_01 AC 20 ms
8,832 KB
testcase_02 AC 20 ms
8,652 KB
testcase_03 AC 20 ms
8,796 KB
testcase_04 AC 22 ms
8,708 KB
testcase_05 AC 20 ms
8,768 KB
testcase_06 AC 20 ms
8,820 KB
testcase_07 AC 20 ms
8,820 KB
testcase_08 AC 20 ms
8,892 KB
testcase_09 AC 21 ms
8,852 KB
testcase_10 AC 20 ms
8,792 KB
testcase_11 AC 19 ms
8,820 KB
testcase_12 AC 20 ms
8,736 KB
testcase_13 AC 19 ms
8,680 KB
testcase_14 AC 20 ms
8,660 KB
testcase_15 AC 20 ms
8,672 KB
testcase_16 AC 20 ms
8,756 KB
testcase_17 AC 20 ms
8,744 KB
testcase_18 AC 20 ms
8,824 KB
testcase_19 AC 20 ms
8,676 KB
testcase_20 AC 19 ms
8,676 KB
testcase_21 AC 20 ms
8,748 KB
testcase_22 AC 20 ms
8,732 KB
testcase_23 AC 20 ms
8,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def scc(N, G, RG):
    """
    https://tjkendev.github.io/procon-library/python/graph/scc.html から拝借しています。
    入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ
    出力: (<ラベル数>, <各頂点のラベル番号>)
    """
    order = []
    used = [0] * N
    group = [None] * N

    def dfs(s):
        used[s] = 1
        for t in G[s]:
            if not used[t]:
                dfs(t)
        order.append(s)

    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t in RG[s]:
            if not used[t]:
                rdfs(t, col)

    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0] * N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group


# 縮約後のグラフを構築
def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    return G0, GP


N = int(input())
L2 = [0] * N
G, RG = [[] for _ in range(N)], [[] for _ in range(N)]
for i in range(N):
    L, S = map(int, input().split())
    L2[i] = L * 2
    S -= 1
    G[S].append(i)
    RG[i].append(S)

label, group = scc(N, G, RG)
G0, GP = construct(N, G, label, group)

diff = [[] for _ in range(label)]
diff_cont = [0] * label
for i, d in zip(group, L2):
    diff[i].append(d)
for i in range(label):
    diff_cont[i] = sum(diff[i]) // 2 + min(diff[i]) // 2

sources = set(range(label))
for st in G0:
    sources -= st

# dfs
ans = 0
nonvisited = [True] * label
for s0 in sources:
    ans += diff_cont[s0]
    d = deque([s0])
    while d:
        v = d.pop()
        for x in G0[v]:
            if nonvisited[x]:
                ans += diff_cont[x] // 2
                d.append(x)
                nonvisited[x] = False

print(f'{ans // 2}.{5 if ans % 2 else 0}')
0