結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | ntuda |
提出日時 | 2024-02-17 20:51:12 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,545 bytes |
コンパイル時間 | 154 ms |
コンパイル使用メモリ | 82,524 KB |
実行使用メモリ | 163,208 KB |
最終ジャッジ日時 | 2024-09-28 23:55:26 |
合計ジャッジ時間 | 33,755 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 918 ms
100,628 KB |
testcase_01 | AC | 1,752 ms
123,076 KB |
testcase_02 | AC | 2,023 ms
155,104 KB |
testcase_03 | AC | 97 ms
78,864 KB |
testcase_04 | AC | 68 ms
73,204 KB |
testcase_05 | AC | 101 ms
79,184 KB |
testcase_06 | AC | 659 ms
90,112 KB |
testcase_07 | AC | 1,731 ms
159,972 KB |
testcase_08 | AC | 1,666 ms
151,348 KB |
testcase_09 | AC | 1,981 ms
161,376 KB |
testcase_10 | TLE | - |
testcase_11 | AC | 2,438 ms
162,288 KB |
testcase_12 | AC | 2,397 ms
163,208 KB |
testcase_13 | TLE | - |
testcase_14 | AC | 2,396 ms
162,512 KB |
testcase_15 | AC | 878 ms
82,520 KB |
testcase_16 | AC | 830 ms
82,488 KB |
testcase_17 | AC | 846 ms
82,856 KB |
testcase_18 | AC | 981 ms
102,752 KB |
testcase_19 | AC | 1,366 ms
141,764 KB |
testcase_20 | AC | 1,272 ms
131,244 KB |
testcase_21 | AC | 1,567 ms
161,496 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()) AB = [list(map(int,input().split())) for _ in range(M)] C = list(map(int,input().split())) W = list(map(int,input().split())) SW = sum(W) X = [0] * 1024 dsu = [DSU(N) for _ in range(2**10)] for i in range(1,1024): x = i cs = set() tmp = 0 for j in range(10): if x & 1: cs.add(j) tmp += W[j] x >>= 1 X[i] = tmp for a,b in AB: a -= 1 b -= 1 if C[a]-1 in cs and C[b]-1 in cs: dsu[i].merge(a,b) for _ in range(int(input())): u,v = map(int,input().split()) ans = SW + 1 for i in range(1, 1024): if dsu[i].same(u-1,v-1): ans = min(ans,X[i]) if ans == SW + 1: print(-1) else: print(ans)