結果
問題 | No.317 辺の追加 |
ユーザー | rpy3cpp |
提出日時 | 2016-02-29 23:05:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 360 ms / 2,000 ms |
コード長 | 2,371 bytes |
コンパイル時間 | 406 ms |
コンパイル使用メモリ | 82,916 KB |
実行使用メモリ | 82,916 KB |
最終ジャッジ日時 | 2024-09-24 13:01:58 |
合計ジャッジ時間 | 11,164 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
52,748 KB |
testcase_01 | AC | 38 ms
53,148 KB |
testcase_02 | AC | 284 ms
81,064 KB |
testcase_03 | AC | 297 ms
80,772 KB |
testcase_04 | AC | 289 ms
82,080 KB |
testcase_05 | AC | 290 ms
79,888 KB |
testcase_06 | AC | 331 ms
80,888 KB |
testcase_07 | AC | 209 ms
78,404 KB |
testcase_08 | AC | 300 ms
82,420 KB |
testcase_09 | AC | 318 ms
82,612 KB |
testcase_10 | AC | 356 ms
82,692 KB |
testcase_11 | AC | 137 ms
82,004 KB |
testcase_12 | AC | 349 ms
82,624 KB |
testcase_13 | AC | 181 ms
82,356 KB |
testcase_14 | AC | 325 ms
82,516 KB |
testcase_15 | AC | 348 ms
82,444 KB |
testcase_16 | AC | 313 ms
82,916 KB |
testcase_17 | AC | 335 ms
82,392 KB |
testcase_18 | AC | 360 ms
82,596 KB |
testcase_19 | AC | 348 ms
82,772 KB |
testcase_20 | AC | 304 ms
82,756 KB |
testcase_21 | AC | 110 ms
79,020 KB |
testcase_22 | AC | 97 ms
78,048 KB |
testcase_23 | AC | 100 ms
78,036 KB |
testcase_24 | AC | 132 ms
80,752 KB |
testcase_25 | AC | 119 ms
79,456 KB |
testcase_26 | AC | 120 ms
79,692 KB |
testcase_27 | AC | 110 ms
78,956 KB |
testcase_28 | AC | 99 ms
78,176 KB |
testcase_29 | AC | 86 ms
76,964 KB |
testcase_30 | AC | 224 ms
81,308 KB |
testcase_31 | AC | 214 ms
81,976 KB |
testcase_32 | AC | 213 ms
81,364 KB |
testcase_33 | AC | 213 ms
81,768 KB |
testcase_34 | AC | 211 ms
81,540 KB |
testcase_35 | AC | 214 ms
81,380 KB |
testcase_36 | AC | 212 ms
81,568 KB |
testcase_37 | AC | 212 ms
81,452 KB |
testcase_38 | AC | 222 ms
81,720 KB |
testcase_39 | AC | 214 ms
81,820 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) 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)