結果

問題 No.19 ステージの選択
ユーザー maspy
提出日時 2020-03-24 15:12:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 742 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 56,280 KB
最終ジャッジ日時 2024-12-31 15:03:46
合計ジャッジ時間 29,706 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 24
権限があれば一括ダウンロードができます

ソースコード

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