結果

問題 No.317 辺の追加
ユーザー tktk_snsntktk_snsn
提出日時 2021-09-23 00:50:55
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,544 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 71,756 KB
最終ジャッジ日時 2023-09-18 19:45:03
合計ジャッジ時間 6,894 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,252 KB
testcase_01 AC 91 ms
71,756 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

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

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