結果

問題 No.317 辺の追加
ユーザー maspymaspy
提出日時 2020-03-31 21:04:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 596 ms / 2,000 ms
コード長 842 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 10,776 KB
実行使用メモリ 89,804 KB
最終ジャッジ日時 2023-09-07 08:36:03
合計ジャッジ時間 23,566 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 204 ms
43,388 KB
testcase_01 AC 197 ms
43,352 KB
testcase_02 AC 431 ms
67,208 KB
testcase_03 AC 484 ms
75,700 KB
testcase_04 AC 393 ms
64,728 KB
testcase_05 AC 486 ms
81,300 KB
testcase_06 AC 530 ms
86,820 KB
testcase_07 AC 308 ms
55,836 KB
testcase_08 AC 406 ms
66,596 KB
testcase_09 AC 478 ms
73,256 KB
testcase_10 AC 588 ms
88,784 KB
testcase_11 AC 321 ms
62,940 KB
testcase_12 AC 587 ms
88,792 KB
testcase_13 AC 345 ms
64,016 KB
testcase_14 AC 526 ms
76,652 KB
testcase_15 AC 567 ms
85,248 KB
testcase_16 AC 431 ms
68,732 KB
testcase_17 AC 537 ms
80,720 KB
testcase_18 AC 587 ms
88,496 KB
testcase_19 AC 596 ms
89,804 KB
testcase_20 AC 409 ms
67,368 KB
testcase_21 AC 322 ms
57,240 KB
testcase_22 AC 264 ms
52,588 KB
testcase_23 AC 269 ms
52,608 KB
testcase_24 AC 409 ms
68,412 KB
testcase_25 AC 349 ms
60,848 KB
testcase_26 AC 357 ms
61,748 KB
testcase_27 AC 324 ms
57,988 KB
testcase_28 AC 269 ms
52,432 KB
testcase_29 AC 217 ms
45,604 KB
testcase_30 AC 459 ms
71,220 KB
testcase_31 AC 462 ms
71,452 KB
testcase_32 AC 460 ms
71,208 KB
testcase_33 AC 470 ms
71,496 KB
testcase_34 AC 463 ms
71,408 KB
testcase_35 AC 464 ms
71,380 KB
testcase_36 AC 462 ms
71,492 KB
testcase_37 AC 465 ms
71,288 KB
testcase_38 AC 467 ms
71,356 KB
testcase_39 AC 469 ms
71,080 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 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