結果

問題 No.317 辺の追加
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-29 19:12:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,233 bytes
コンパイル時間 116 ms
コンパイル使用メモリ 12,136 KB
実行使用メモリ 20,796 KB
最終ジャッジ日時 2023-10-24 19:12:46
合計ジャッジ時間 35,718 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,368 KB
testcase_01 AC 30 ms
10,368 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 TLE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 249 ms
14,372 KB
testcase_22 AC 148 ms
12,536 KB
testcase_23 WA -
testcase_24 AC 402 ms
17,340 KB
testcase_25 AC 301 ms
15,332 KB
testcase_26 AC 317 ms
15,616 KB
testcase_27 AC 263 ms
14,504 KB
testcase_28 WA -
testcase_29 AC 57 ms
10,928 KB
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
権限があれば一括ダウンロードができます

ソースコード

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)
    for v, m in zip(vs, ms):
        update_dp(v, m, dp, N)
    return dp

def update_dp(v, m, dp, N):
    pow2 = 1
    while pow2 <= m:
        if m & pow2:
            update_dp_core(v * pow2, pow2, dp, N)
        pow2 <<= 1

def update_dp_core(v, w, dp, N):
    for i in range(N - v, 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