結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | tassei903 |
提出日時 | 2024-02-11 14:23:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,208 ms / 2,500 ms |
コード長 | 2,266 bytes |
コンパイル時間 | 323 ms |
コンパイル使用メモリ | 81,572 KB |
実行使用メモリ | 160,396 KB |
最終ジャッジ日時 | 2024-02-11 16:14:30 |
合計ジャッジ時間 | 26,173 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 676 ms
98,216 KB |
testcase_01 | AC | 1,279 ms
119,960 KB |
testcase_02 | AC | 1,616 ms
151,600 KB |
testcase_03 | AC | 69 ms
72,616 KB |
testcase_04 | AC | 51 ms
64,172 KB |
testcase_05 | AC | 79 ms
76,200 KB |
testcase_06 | AC | 564 ms
86,788 KB |
testcase_07 | AC | 1,357 ms
156,132 KB |
testcase_08 | AC | 1,301 ms
147,428 KB |
testcase_09 | AC | 1,576 ms
157,412 KB |
testcase_10 | AC | 2,208 ms
160,140 KB |
testcase_11 | AC | 1,810 ms
159,944 KB |
testcase_12 | AC | 1,825 ms
160,176 KB |
testcase_13 | AC | 1,851 ms
160,088 KB |
testcase_14 | AC | 1,878 ms
160,396 KB |
testcase_15 | AC | 630 ms
79,688 KB |
testcase_16 | AC | 614 ms
79,308 KB |
testcase_17 | AC | 637 ms
79,680 KB |
testcase_18 | AC | 812 ms
100,240 KB |
testcase_19 | AC | 1,029 ms
138,980 KB |
testcase_20 | AC | 955 ms
127,588 KB |
testcase_21 | AC | 1,164 ms
157,540 KB |
ソースコード
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes") no = lambda :print("no");No = lambda :print("No") ####################################################################### from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N, M = na() D = 10 E = [] for _ in range(M): x, y = na() E.append((x-1, y-1)) c = [x-1 for x in na()] s = na() score = [0] * (1<<D) for bit in range(1<<D): for i in range(D): if (bit>>i)&1: score[bit] += s[i] ufs = [UnionFind(N) for _ in range(1<<D)] for bit in range(1<<D): for i, j in E: if (bit>>c[i])&1 and (bit>>c[j])&1: ufs[bit].union(i, j) Q = ni() inf = 10**18 for _ in range(Q): x, y = na() x -= 1 y -= 1 ans = inf for bit in range(1<<D): if ufs[bit].same(x, y): #print(bin(bit)) ans = min(ans, score[bit]) print(ans if ans != inf else -1)