結果

問題 No.317 辺の追加
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-29 19:27:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,323 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 11,956 KB
実行使用メモリ 22,132 KB
最終ジャッジ日時 2023-10-24 19:12:57
合計ジャッジ時間 8,552 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,236 KB
testcase_01 AC 27 ms
10,236 KB
testcase_02 AC 456 ms
16,508 KB
testcase_03 AC 494 ms
15,480 KB
testcase_04 AC 1,104 ms
17,848 KB
testcase_05 AC 520 ms
13,808 KB
testcase_06 AC 612 ms
15,452 KB
testcase_07 AC 212 ms
12,084 KB
testcase_08 TLE -
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 #

class DisjointSet(object):
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
        self.num = n  # number of disjoint sets

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

    def _link(self, x, y):
        if x == y:
            return
        self.num -= 1
        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:]:
        print(w - 1)
    
def solve_min_coins(vs, ms, N):
    '''
    len(vs) 種類のコインがある。
    コインの額面は vs[i], コインの枚数は ms[i]
    sum(v * m for v, m in zip(vs, ms)) == N <= 10**5
    が成り立つ。
    1 から N 円までの各金額をコインであらわすとき、各金額における最小コイン枚数はいくつか?    
    '''
    dp = [0] * (N + 1)
    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):
    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, 0, -1):
        if dp[i]:
            dpiv = dp[i + v]
            if dp[i] + w < dpiv or dpiv == 0:
                dp[i + v] = dp[i] + w
    if dp[v] > w or dp[v] == 0:
        dp[v] = w

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