結果
問題 | No.317 辺の追加 |
ユーザー | rpy3cpp |
提出日時 | 2016-02-29 23:30:32 |
言語 | PyPy2 (7.3.15) |
結果 |
AC
|
実行時間 | 455 ms / 2,000 ms |
コード長 | 2,413 bytes |
コンパイル時間 | 598 ms |
コンパイル使用メモリ | 76,820 KB |
実行使用メモリ | 85,820 KB |
最終ジャッジ日時 | 2024-09-24 12:56:13 |
合計ジャッジ時間 | 14,567 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 76 ms
75,304 KB |
testcase_01 | AC | 74 ms
75,560 KB |
testcase_02 | AC | 455 ms
84,476 KB |
testcase_03 | AC | 355 ms
83,340 KB |
testcase_04 | AC | 346 ms
84,620 KB |
testcase_05 | AC | 405 ms
82,460 KB |
testcase_06 | AC | 385 ms
83,472 KB |
testcase_07 | AC | 249 ms
80,668 KB |
testcase_08 | AC | 363 ms
84,640 KB |
testcase_09 | AC | 378 ms
85,648 KB |
testcase_10 | AC | 430 ms
85,116 KB |
testcase_11 | AC | 180 ms
84,608 KB |
testcase_12 | AC | 425 ms
85,020 KB |
testcase_13 | AC | 230 ms
84,648 KB |
testcase_14 | AC | 403 ms
85,124 KB |
testcase_15 | AC | 424 ms
85,172 KB |
testcase_16 | AC | 368 ms
85,012 KB |
testcase_17 | AC | 410 ms
85,164 KB |
testcase_18 | AC | 440 ms
85,820 KB |
testcase_19 | AC | 436 ms
85,404 KB |
testcase_20 | AC | 361 ms
85,440 KB |
testcase_21 | AC | 156 ms
81,708 KB |
testcase_22 | AC | 142 ms
80,316 KB |
testcase_23 | AC | 142 ms
80,540 KB |
testcase_24 | AC | 185 ms
83,360 KB |
testcase_25 | AC | 175 ms
82,212 KB |
testcase_26 | AC | 173 ms
82,680 KB |
testcase_27 | AC | 158 ms
81,692 KB |
testcase_28 | AC | 143 ms
80,288 KB |
testcase_29 | AC | 125 ms
79,660 KB |
testcase_30 | AC | 279 ms
84,372 KB |
testcase_31 | AC | 269 ms
84,376 KB |
testcase_32 | AC | 270 ms
84,396 KB |
testcase_33 | AC | 266 ms
84,348 KB |
testcase_34 | AC | 270 ms
84,364 KB |
testcase_35 | AC | 270 ms
84,656 KB |
testcase_36 | AC | 267 ms
84,376 KB |
testcase_37 | AC | 266 ms
84,248 KB |
testcase_38 | AC | 277 ms
84,136 KB |
testcase_39 | AC | 272 ms
84,508 KB |
ソースコード
# 3to2 range = xrange input = raw_input 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) n1 = N + 1 for w in dp[1:]: print((w % n1) - 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 if v == 2: if dp[1] == 1: fill_dp_2a(v, m, dp, cum) else: fill_dp_2b(v, m, dp, cum) 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 fill_dp_2a(v, m, dp, cum): for i in range(m + 1): dp[i * 2] = i dp[i * 2 + 1] = i + 1 for i in range(m * 2 + 2, cum + m * 2 + 1): dp[i] = i - m def fill_dp_2b(v, m, dp, cum): for i in range(m + 1): dp[i * 2] = i def update_dp_core(v, w, dp, cum): for i in range(cum, -1, -1): iv = i + v if dp[i] + w < dp[iv]: dp[iv] = dp[i] + w N, ds = read_data() solve2(N, ds)