結果

問題 No.19 ステージの選択
ユーザー maspymaspy
提出日時 2023-06-11 23:43:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 217 ms / 5,000 ms
コード長 743 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 43,120 KB
最終ジャッジ日時 2023-09-01 03:01:18
合計ジャッジ時間 12,288 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 215 ms
42,832 KB
testcase_01 AC 210 ms
42,728 KB
testcase_02 AC 210 ms
42,792 KB
testcase_03 AC 206 ms
42,840 KB
testcase_04 AC 210 ms
43,120 KB
testcase_05 AC 208 ms
43,012 KB
testcase_06 AC 208 ms
42,844 KB
testcase_07 AC 213 ms
42,956 KB
testcase_08 AC 217 ms
42,980 KB
testcase_09 AC 208 ms
42,952 KB
testcase_10 AC 207 ms
42,588 KB
testcase_11 AC 208 ms
42,724 KB
testcase_12 AC 210 ms
42,660 KB
testcase_13 AC 208 ms
42,596 KB
testcase_14 AC 209 ms
42,724 KB
testcase_15 AC 210 ms
42,620 KB
testcase_16 AC 209 ms
42,844 KB
testcase_17 AC 209 ms
42,732 KB
testcase_18 AC 203 ms
42,908 KB
testcase_19 AC 208 ms
43,048 KB
testcase_20 AC 204 ms
42,564 KB
testcase_21 AC 207 ms
42,880 KB
testcase_22 AC 201 ms
42,848 KB
testcase_23 AC 203 ms
43,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from scipy.sparse.csgraph import connected_components
import numpy as np
N = int(readline())
LS = np.array(read().split(), np.int32)
L = LS[::2]
S = LS[1::2] - 1

graph = np.zeros((N, N), np.bool_)
graph[np.arange(N), S] = 1

Ncomp, comp = connected_components(graph, connection='strong')

out_deg = [0] * Ncomp
for i, s in enumerate(S):
    ci = comp[i]
    cs = comp[s]
    if ci == cs:
        continue
    out_deg[ci] += 1

answer = 0
for i in range(Ncomp):
    V = np.where(comp == i)[0]
    x = L[V].sum()
    y = L[V].min()
    if out_deg[i] == 0:
        x += y
    answer += x

print(answer / 2)
0