結果

問題 No.317 辺の追加
ユーザー maspymaspy
提出日時 2020-03-31 21:04:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 842 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 126,024 KB
最終ジャッジ日時 2024-06-25 02:56:04
合計ジャッジ時間 56,486 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 TLE * 1
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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 import csr_matrix
from scipy.sparse.csgraph import connected_components
import numpy as np

N, M = map(int, readline().split())
m = map(int, read().split())
U, V = zip(*zip(m, m))
G = csr_matrix(([1] * M, (U, V)), (N + 1, N + 1))
_, comp = connected_components(G, directed=False)
_, counts = np.unique(comp[1:], return_counts=True)
counts.sort()
size, cnt = np.unique(counts, return_counts=True)

INF = 10 ** 6
dp = np.full(N + 1, INF, np.int32)
dp[0] = -1
for s, c in zip(size, cnt):
    cc = 1
    while c:
        c -= cc
        ss = cc * s
        np.minimum(dp[ss:], dp[:-ss] + cc, out=dp[ss:])
        cc = min(2 * cc, c)

dp[dp == INF] = -1
print('\n'.join(dp[1:].astype(str)))
0