結果
| 問題 |
No.2630 Colorful Vertices and Cheapest Paths
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-18 17:43:14 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,625 bytes |
| コンパイル時間 | 189 ms |
| コンパイル使用メモリ | 82,228 KB |
| 実行使用メモリ | 165,876 KB |
| 最終ジャッジ日時 | 2024-09-29 00:44:42 |
| 合計ジャッジ時間 | 25,413 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 TLE * 10 |
ソースコード
class UnionFind:
def __init__(self, n):
self.n = n
self.par = [-1] * n
self.group_ = n
def find(self, x):
if self.par[x] < 0:
return x
lst = []
while self.par[x] >= 0:
lst.append(x)
x = self.par[x]
for y in lst:
self.par[y] = x
return x
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
if self.par[x] > self.par[y]:
x, y = y, x
self.par[x] += self.par[y]
self.par[y] = x
self.group_ -= 1
return True
def size(self, x):
return -self.par[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
@property
def group(self):
return self.group_
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()))
t = 10
UF = [UnionFind(n) for _ in range(1 << t)]
cost = [0] * (1 << t)
for bit in range(1 << t):
for i in range(t):
if bit >> i & 1:
cost[bit] += W[i]
for a, b in ab:
a -= 1
b -= 1
bit = (1 << C[a] - 1) | (1 << C[b] - 1)
for S in range(1 << t):
if S & bit == bit:
UF[S].unite(a, b)
Q = int(input())
for _ in range(Q):
x, y = map(int, input().split())
x -= 1
y -= 1
ans = cost[-1] + 1
for S in range(1 << t):
if UF[S].same(x, y):
ans = min(ans, cost[S])
if ans == cost[-1] + 1:
ans = -1
print(ans)