結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | hato336 |
提出日時 | 2024-02-16 21:38:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,112 ms / 2,500 ms |
コード長 | 2,390 bytes |
コンパイル時間 | 233 ms |
コンパイル使用メモリ | 82,712 KB |
実行使用メモリ | 96,524 KB |
最終ジャッジ日時 | 2024-09-28 19:52:59 |
合計ジャッジ時間 | 17,682 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 582 ms
92,996 KB |
testcase_01 | AC | 895 ms
94,536 KB |
testcase_02 | AC | 940 ms
94,740 KB |
testcase_03 | AC | 151 ms
90,100 KB |
testcase_04 | AC | 134 ms
89,716 KB |
testcase_05 | AC | 157 ms
90,384 KB |
testcase_06 | AC | 541 ms
92,188 KB |
testcase_07 | AC | 764 ms
93,276 KB |
testcase_08 | AC | 725 ms
93,164 KB |
testcase_09 | AC | 788 ms
93,552 KB |
testcase_10 | AC | 1,100 ms
96,524 KB |
testcase_11 | AC | 1,086 ms
96,324 KB |
testcase_12 | AC | 1,106 ms
96,224 KB |
testcase_13 | AC | 1,087 ms
96,092 KB |
testcase_14 | AC | 1,112 ms
96,300 KB |
testcase_15 | AC | 747 ms
92,916 KB |
testcase_16 | AC | 747 ms
92,796 KB |
testcase_17 | AC | 770 ms
93,552 KB |
testcase_18 | AC | 553 ms
92,284 KB |
testcase_19 | AC | 701 ms
93,248 KB |
testcase_20 | AC | 649 ms
92,836 KB |
testcase_21 | AC | 746 ms
93,980 KB |
ソースコード
import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random #sys.setrecursionlimit(10**9) #sys.set_int_max_str_digits(0) input = sys.stdin.readline #n,m = map(int,input().split()) #for i in range(n): # alist.append(list(map(int,input().split()))) 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 = collections.defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items()) n,m = map(int,input().split()) edge = [[] for i in range(n)] e = [] for i in range(m): u,v = map(int,input().split()) u-=1 v-=1 e.append((u,v)) edge[u].append(v) edge[v].append(u) color = list(map(lambda x:int(x)-1,input().split())) w = list(map(int,input().split())) q = int(input()) query = [list(map(lambda x:int(x)-1,input().split())) for i in range(q)] ans = [10**18 for i in range(q)] for i in itertools.product([0,1],repeat=10): uf = UnionFind(n) cost = sum(w[j] if i[j] == 1 else 0 for j in range(10)) for u,v in e: if i[color[u]] == 1 and i[color[v]] == 1: uf.union(u,v) for j in range(q): x,y = query[j] if i[color[x]] == 1 and i[color[y]] == 1 and uf.same(x,y): ans[j] = min(ans[j],cost) for i in ans: print(-1 if i == 10**18 else i)