結果

問題 No.317 辺の追加
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-29 22:44:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 1,984 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 82,668 KB
最終ジャッジ日時 2023-10-24 19:15:59
合計ジャッジ時間 11,753 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,540 KB
testcase_01 AC 39 ms
53,540 KB
testcase_02 AC 316 ms
81,604 KB
testcase_03 AC 324 ms
80,552 KB
testcase_04 AC 316 ms
81,868 KB
testcase_05 AC 306 ms
79,532 KB
testcase_06 AC 346 ms
80,488 KB
testcase_07 AC 224 ms
78,068 KB
testcase_08 AC 331 ms
82,132 KB
testcase_09 AC 344 ms
82,164 KB
testcase_10 AC 384 ms
82,396 KB
testcase_11 AC 146 ms
81,604 KB
testcase_12 AC 376 ms
82,228 KB
testcase_13 AC 191 ms
81,412 KB
testcase_14 AC 356 ms
82,196 KB
testcase_15 AC 372 ms
82,388 KB
testcase_16 AC 341 ms
82,104 KB
testcase_17 AC 366 ms
82,328 KB
testcase_18 AC 389 ms
82,668 KB
testcase_19 AC 385 ms
82,396 KB
testcase_20 AC 330 ms
82,396 KB
testcase_21 AC 119 ms
78,676 KB
testcase_22 AC 110 ms
77,680 KB
testcase_23 AC 111 ms
77,844 KB
testcase_24 AC 135 ms
80,500 KB
testcase_25 AC 129 ms
79,372 KB
testcase_26 AC 129 ms
79,548 KB
testcase_27 AC 122 ms
78,876 KB
testcase_28 AC 110 ms
77,808 KB
testcase_29 AC 91 ms
76,528 KB
testcase_30 AC 213 ms
81,216 KB
testcase_31 AC 227 ms
81,244 KB
testcase_32 AC 206 ms
81,296 KB
testcase_33 AC 229 ms
81,244 KB
testcase_34 AC 204 ms
81,268 KB
testcase_35 AC 228 ms
81,240 KB
testcase_36 AC 206 ms
81,256 KB
testcase_37 AC 208 ms
81,252 KB
testcase_38 AC 212 ms
81,244 KB
testcase_39 AC 226 ms
81,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DisjointSet(object):
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n

    def union(self, x, y):
        self._link(self.find_set(x), self.find_set(y))

    def _link(self, x, y):
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
        else:
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def find_set(self, x):
        xp = self.parent[x]
        if xp != x:
            self.parent[x] = self.find_set(xp)
        return self.parent[x]


def read_data():
    N, M = map(int, input().split())
    ds = DisjointSet(N)
    for m in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        if u == v:
            continue
        ds.union(u, v)
    return N, ds


def solve2(N, ds):
    counts = [0] * N
    for i in range(N):
        counts[ds.find_set(i)] += 1
    freqs = [0] * (N + 1)
    for c in counts:
        freqs[c] += 1
    vs = []
    ms = []
    for i, f in enumerate(freqs):
        if f and i:
            vs.append(i)
            ms.append(f)
    dp = solve_min_coins(vs, ms, N)
    for w in dp[1:]:
        if w == N + 1:
            print(-1)
        else:
            print(w - 1)
    
def solve_min_coins(vs, ms, N):
    dp = [N + 1] * (N + 1)
    dp[0] = 0
    cum = 0
    for v, m in zip(vs, ms):
        cum = update_dp(v, m, dp, cum)
    return dp


def update_dp(v, m, dp, cum):
    if v == 1:
        for i in range(m + 1):
            dp[i] = i
        return cum + v * m
    pow2 = 1
    while pow2 < m:
        update_dp_core(v * pow2, pow2, dp, cum)
        cum += v * pow2
        m -= pow2
        pow2 <<= 1
    update_dp_core(v * m, m, dp, cum)
    return cum + v * m


def update_dp_core(v, w, dp, cum):
    for i in range(cum, -1, -1):
        if dp[i] + w < dp[i + v]:
            dp[i + v] = dp[i] + w

N, ds = read_data()
solve2(N, ds)
0