結果

問題 No.317 辺の追加
ユーザー tktk_snsntktk_snsn
提出日時 2021-09-23 00:57:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 535 ms / 2,000 ms
コード長 1,560 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 92,184 KB
最終ジャッジ日時 2023-09-18 19:45:33
合計ジャッジ時間 14,999 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,740 KB
testcase_01 AC 95 ms
71,328 KB
testcase_02 AC 288 ms
80,824 KB
testcase_03 AC 268 ms
80,596 KB
testcase_04 AC 380 ms
82,876 KB
testcase_05 AC 244 ms
79,948 KB
testcase_06 AC 276 ms
80,264 KB
testcase_07 AC 195 ms
79,160 KB
testcase_08 AC 535 ms
85,568 KB
testcase_09 AC 291 ms
81,680 KB
testcase_10 AC 305 ms
81,312 KB
testcase_11 AC 237 ms
89,680 KB
testcase_12 AC 304 ms
81,020 KB
testcase_13 AC 304 ms
92,184 KB
testcase_14 AC 291 ms
81,672 KB
testcase_15 AC 301 ms
81,512 KB
testcase_16 AC 357 ms
83,212 KB
testcase_17 AC 294 ms
81,496 KB
testcase_18 AC 305 ms
81,644 KB
testcase_19 AC 308 ms
81,508 KB
testcase_20 AC 417 ms
83,572 KB
testcase_21 AC 153 ms
79,076 KB
testcase_22 AC 145 ms
78,364 KB
testcase_23 AC 152 ms
78,692 KB
testcase_24 AC 168 ms
79,888 KB
testcase_25 AC 164 ms
79,700 KB
testcase_26 AC 166 ms
79,840 KB
testcase_27 AC 162 ms
79,340 KB
testcase_28 AC 150 ms
78,752 KB
testcase_29 AC 128 ms
78,480 KB
testcase_30 AC 388 ms
80,500 KB
testcase_31 AC 390 ms
80,472 KB
testcase_32 AC 391 ms
80,820 KB
testcase_33 AC 392 ms
80,456 KB
testcase_34 AC 389 ms
80,612 KB
testcase_35 AC 392 ms
80,504 KB
testcase_36 AC 387 ms
80,564 KB
testcase_37 AC 392 ms
80,336 KB
testcase_38 AC 419 ms
80,532 KB
testcase_39 AC 386 ms
80,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 9


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        stack = []
        while self.root[x] >= 0:
            stack.append(x)
            x = self.root[x]
        for i in stack:
            self.root[i] = x
        return x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

    def size(self, x):
        return -self.root[self.find(x)]


N, M = map(int, input().split())
uf = UF_tree(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.unite(a-1, b-1)

leader = {uf.find(i) for i in range(N)}
cnt = [uf.size(i) for i in leader]

dp = [inf] * (N + 1)
dp[0] = 0
for W, total in Counter(cnt).items():
    k = 1
    while total > 0:
        mul = min(k, total)
        for i in reversed(range(W * mul, N+1)):
            dp[i] = min(dp[i], dp[i - W * mul] + mul)
        total -= mul
        k *= 2

for i in range(1, N+1):
    if dp[i] > N + 5:
        print(-1)
    else:
        print(dp[i] - 1)
0