結果

問題 No.317 辺の追加
ユーザー 👑 rin204rin204
提出日時 2022-11-11 16:52:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 460 ms / 2,000 ms
コード長 1,838 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 85,448 KB
最終ジャッジ日時 2023-10-11 13:26:33
合計ジャッジ時間 15,005 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,432 KB
testcase_01 AC 75 ms
71,600 KB
testcase_02 AC 331 ms
80,904 KB
testcase_03 AC 327 ms
80,144 KB
testcase_04 AC 365 ms
80,616 KB
testcase_05 AC 303 ms
79,476 KB
testcase_06 AC 344 ms
80,980 KB
testcase_07 AC 226 ms
79,236 KB
testcase_08 AC 460 ms
82,804 KB
testcase_09 AC 367 ms
82,152 KB
testcase_10 AC 384 ms
81,416 KB
testcase_11 AC 211 ms
85,448 KB
testcase_12 AC 378 ms
81,244 KB
testcase_13 AC 262 ms
83,076 KB
testcase_14 AC 377 ms
82,420 KB
testcase_15 AC 399 ms
81,396 KB
testcase_16 AC 391 ms
81,736 KB
testcase_17 AC 390 ms
81,544 KB
testcase_18 AC 392 ms
81,248 KB
testcase_19 AC 399 ms
81,484 KB
testcase_20 AC 401 ms
82,440 KB
testcase_21 AC 149 ms
78,620 KB
testcase_22 AC 138 ms
78,188 KB
testcase_23 AC 147 ms
78,720 KB
testcase_24 AC 165 ms
78,980 KB
testcase_25 AC 159 ms
78,760 KB
testcase_26 AC 161 ms
79,440 KB
testcase_27 AC 155 ms
78,876 KB
testcase_28 AC 143 ms
78,548 KB
testcase_29 AC 121 ms
77,556 KB
testcase_30 AC 375 ms
79,308 KB
testcase_31 AC 292 ms
79,632 KB
testcase_32 AC 290 ms
79,732 KB
testcase_33 AC 291 ms
79,748 KB
testcase_34 AC 290 ms
79,464 KB
testcase_35 AC 293 ms
79,428 KB
testcase_36 AC 297 ms
79,708 KB
testcase_37 AC 294 ms
79,584 KB
testcase_38 AC 349 ms
79,712 KB
testcase_39 AC 287 ms
79,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return False
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m = map(int, input().split())
UF = UnionFind(n)
for _ in range(m):
    u, v = map(int, input().split())
    UF.union(u - 1, v - 1)

cnt = {}
for r in UF.roots():
    s = UF.size(r)
    cnt[s] = cnt.get(s, 0) + 1

dp = [1 << 60] * (n + 1)
dp[0] = 0
for k, v in cnt.items():
    c = 1
    while v > 0:
        w = min(c, v)
        v -= c
        c *= 2

        for i in range(n, k * w - 1, -1):
            dp[i] = min(dp[i], dp[i - k * w] + w)

for i in range(1, n + 1):
    if dp[i] == 1 << 60:
        print(-1)
    else:
        print(dp[i] - 1)
0