結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー 👑 rin204rin204
提出日時 2024-02-18 17:43:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,625 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 158,848 KB
最終ジャッジ日時 2024-02-18 17:44:04
合計ジャッジ時間 45,490 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 960 ms
96,512 KB
testcase_01 AC 2,398 ms
119,140 KB
testcase_02 TLE -
testcase_03 AC 52 ms
70,680 KB
testcase_04 AC 38 ms
62,092 KB
testcase_05 AC 55 ms
72,756 KB
testcase_06 AC 1,169 ms
86,780 KB
testcase_07 TLE -
testcase_08 AC 2,442 ms
148,224 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,736 ms
79,488 KB
testcase_16 AC 1,704 ms
79,616 KB
testcase_17 AC 1,707 ms
79,488 KB
testcase_18 AC 1,404 ms
100,096 KB
testcase_19 AC 1,813 ms
139,392 KB
testcase_20 AC 1,685 ms
128,000 KB
testcase_21 AC 2,128 ms
157,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0