結果
問題 | No.317 辺の追加 |
ユーザー | rpy3cpp |
提出日時 | 2016-02-29 22:37:03 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 345 ms / 2,000 ms |
コード長 | 2,015 bytes |
コンパイル時間 | 264 ms |
コンパイル使用メモリ | 82,256 KB |
実行使用メモリ | 82,920 KB |
最終ジャッジ日時 | 2024-09-24 12:56:52 |
合計ジャッジ時間 | 11,343 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
52,620 KB |
testcase_01 | AC | 36 ms
53,372 KB |
testcase_02 | AC | 277 ms
81,232 KB |
testcase_03 | AC | 287 ms
80,916 KB |
testcase_04 | AC | 277 ms
82,072 KB |
testcase_05 | AC | 265 ms
79,624 KB |
testcase_06 | AC | 301 ms
80,640 KB |
testcase_07 | AC | 198 ms
78,316 KB |
testcase_08 | AC | 309 ms
82,852 KB |
testcase_09 | AC | 301 ms
82,632 KB |
testcase_10 | AC | 332 ms
82,716 KB |
testcase_11 | AC | 138 ms
81,736 KB |
testcase_12 | AC | 345 ms
82,388 KB |
testcase_13 | AC | 186 ms
81,704 KB |
testcase_14 | AC | 320 ms
82,504 KB |
testcase_15 | AC | 338 ms
82,488 KB |
testcase_16 | AC | 305 ms
82,592 KB |
testcase_17 | AC | 325 ms
82,396 KB |
testcase_18 | AC | 343 ms
82,756 KB |
testcase_19 | AC | 332 ms
82,920 KB |
testcase_20 | AC | 298 ms
82,616 KB |
testcase_21 | AC | 103 ms
78,708 KB |
testcase_22 | AC | 93 ms
77,864 KB |
testcase_23 | AC | 96 ms
78,104 KB |
testcase_24 | AC | 121 ms
81,208 KB |
testcase_25 | AC | 113 ms
79,688 KB |
testcase_26 | AC | 111 ms
79,776 KB |
testcase_27 | AC | 105 ms
78,960 KB |
testcase_28 | AC | 93 ms
78,216 KB |
testcase_29 | AC | 79 ms
76,656 KB |
testcase_30 | AC | 341 ms
81,368 KB |
testcase_31 | AC | 223 ms
81,540 KB |
testcase_32 | AC | 309 ms
81,568 KB |
testcase_33 | AC | 212 ms
81,348 KB |
testcase_34 | AC | 301 ms
81,660 KB |
testcase_35 | AC | 215 ms
81,432 KB |
testcase_36 | AC | 220 ms
81,508 KB |
testcase_37 | AC | 247 ms
81,668 KB |
testcase_38 | AC | 221 ms
81,508 KB |
testcase_39 | AC | 309 ms
81,444 KB |
ソースコード
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:]: print(w - 1) def solve_min_coins(vs, ms, 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): 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, 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)