結果

問題 No.317 辺の追加
ユーザー しらっ亭しらっ亭
提出日時 2015-12-10 17:31:08
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,045 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 87,144 KB
最終ジャッジ日時 2023-10-13 10:34:36
合計ジャッジ時間 5,202 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,776 KB
testcase_01 AC 93 ms
71,680 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


class UnionFind:
    def __init__(self, n):
        self.par = list(range(n))

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        self.par[x] = y


def solve():
    N, M = map(int, input().split())
    uf = UnionFind(N)
    for i in range(M):
        u, v = map(int, input().split())
        uf.union(u - 1, v - 1)

    for i in range(N):
        uf.find(i)

    co = Counter(uf.par)

    gs = []
    for k in co:
        gs.append(co[k])

    inf = float('inf')

    dp = [inf] * (N + 1)
    dp[0] = -1

    for g in gs:
        for i in range(N, -1, -1):
            if dp[i] != inf and i + g <= N:
                dp[i + g] = min(dp[i + g], dp[i] + 1)

    for i in range(1, N + 1):
        print(dp[i] if dp[i] < inf else -1)


if __name__ == '__main__':
    solve()
0