結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | ntuda |
提出日時 | 2024-02-17 20:51:12 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,545 bytes |
コンパイル時間 | 437 ms |
コンパイル使用メモリ | 81,700 KB |
実行使用メモリ | 162,256 KB |
最終ジャッジ日時 | 2024-02-17 20:51:50 |
合計ジャッジ時間 | 36,365 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 979 ms
99,696 KB |
testcase_01 | AC | 1,850 ms
121,824 KB |
testcase_02 | AC | 2,003 ms
154,348 KB |
testcase_03 | AC | 104 ms
78,280 KB |
testcase_04 | AC | 73 ms
73,172 KB |
testcase_05 | AC | 112 ms
78,292 KB |
testcase_06 | AC | 930 ms
89,400 KB |
testcase_07 | AC | 1,769 ms
159,144 KB |
testcase_08 | AC | 1,657 ms
150,568 KB |
testcase_09 | AC | 1,925 ms
160,720 KB |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | AC | 2,450 ms
162,256 KB |
testcase_13 | TLE | - |
testcase_14 | AC | 2,417 ms
161,508 KB |
testcase_15 | AC | 870 ms
81,800 KB |
testcase_16 | AC | 887 ms
81,768 KB |
testcase_17 | AC | 950 ms
81,904 KB |
testcase_18 | AC | 1,117 ms
102,188 KB |
testcase_19 | AC | 1,378 ms
141,224 KB |
testcase_20 | AC | 1,293 ms
130,476 KB |
testcase_21 | AC | 1,577 ms
160,832 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)