結果
問題 | No.2912 0次パーシステントホモロジー |
ユーザー | hiro1729 |
提出日時 | 2024-10-04 22:02:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 740 ms / 2,000 ms |
コード長 | 2,198 bytes |
コンパイル時間 | 200 ms |
コンパイル使用メモリ | 82,084 KB |
実行使用メモリ | 110,364 KB |
最終ジャッジ日時 | 2024-10-04 22:02:35 |
合計ジャッジ時間 | 5,910 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
68,052 KB |
testcase_01 | AC | 54 ms
67,852 KB |
testcase_02 | AC | 54 ms
68,052 KB |
testcase_03 | AC | 53 ms
67,472 KB |
testcase_04 | AC | 54 ms
67,928 KB |
testcase_05 | AC | 59 ms
67,020 KB |
testcase_06 | AC | 55 ms
67,664 KB |
testcase_07 | AC | 53 ms
68,524 KB |
testcase_08 | AC | 54 ms
69,112 KB |
testcase_09 | AC | 59 ms
67,864 KB |
testcase_10 | AC | 55 ms
67,648 KB |
testcase_11 | AC | 55 ms
68,176 KB |
testcase_12 | AC | 54 ms
68,484 KB |
testcase_13 | AC | 61 ms
67,444 KB |
testcase_14 | AC | 59 ms
68,052 KB |
testcase_15 | AC | 89 ms
78,716 KB |
testcase_16 | AC | 322 ms
95,880 KB |
testcase_17 | AC | 339 ms
88,472 KB |
testcase_18 | AC | 436 ms
91,288 KB |
testcase_19 | AC | 667 ms
110,012 KB |
testcase_20 | AC | 740 ms
110,012 KB |
testcase_21 | AC | 503 ms
109,856 KB |
testcase_22 | AC | 512 ms
110,364 KB |
ソースコード
import typing class DSU: ''' Implement (union by size) + (path halving) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a < self._n assert 0 <= b < self._n x = self.leader(a) y = self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a: int, b: int) -> bool: assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a: int) -> int: assert 0 <= a < self._n parent = self.parent_or_size[a] while parent >= 0: if self.parent_or_size[parent] < 0: return parent self.parent_or_size[a], a, parent = ( self.parent_or_size[parent], self.parent_or_size[parent], self.parent_or_size[self.parent_or_size[parent]] ) return a def size(self, a: int) -> int: assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self) -> typing.List[typing.List[int]]: leader_buf = [self.leader(i) for i in range(self._n)] result: typing.List[typing.List[int]] = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return list(filter(lambda r: r, result)) N, M = map(int, input().split()) e = [tuple(map(int, input().split())) for _ in range(M)] e.sort(key = lambda i: i[2]) T = int(input()) R = list(map(int, input().split())) Ri = [(R[i], i) for i in range(T)] Ri.sort() c = N ans = [0] * T uf = DSU(N) d = 0 for r, i in Ri: while d < M and e[d][2] <= r: if not uf.same(e[d][0], e[d][1]): uf.merge(e[d][0], e[d][1]) c -= 1 d += 1 ans[i] = c print(*ans, sep = '\n')