結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー rlangevinrlangevin
提出日時 2024-02-16 22:05:09
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,833 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 162,228 KB
最終ジャッジ日時 2024-02-16 22:05:34
合計ジャッジ時間 24,990 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 520 ms
98,892 KB
testcase_01 AC 881 ms
121,180 KB
testcase_02 TLE -
testcase_03 AC 96 ms
78,024 KB
testcase_04 AC 70 ms
73,152 KB
testcase_05 AC 106 ms
78,156 KB
testcase_06 AC 1,347 ms
88,396 KB
testcase_07 AC 1,827 ms
158,024 KB
testcase_08 AC 1,718 ms
149,320 KB
testcase_09 AC 1,826 ms
160,072 KB
testcase_10 AC 1,539 ms
161,444 KB
testcase_11 AC 1,438 ms
162,076 KB
testcase_12 AC 1,406 ms
162,096 KB
testcase_13 AC 1,364 ms
162,228 KB
testcase_14 AC 1,324 ms
161,928 KB
testcase_15 AC 513 ms
81,276 KB
testcase_16 AC 517 ms
81,420 KB
testcase_17 AC 512 ms
81,396 KB
testcase_18 AC 399 ms
101,832 KB
testcase_19 AC 564 ms
141,000 KB
testcase_20 AC 428 ms
129,224 KB
testcase_21 AC 612 ms
159,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
#https://github.com/not522/ac-library-python/blob/master/atcoder/dsu.py
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())
A, B = [-1] * M, [-1] * M
for i in range(M):
    A[i], B[i] = map(int, input().split())
    A[i], B[i] = A[i] - 1, B[i] - 1
    
C = list(map(int, input().split()))
for i in range(N):
    C[i] -= 1
W = list(map(int, input().split()))
K = 1 << 10
U = [DSU(N) for _ in range(K)]
cost = [0] * K
for s in range(K):
    for a, b in zip(A, B):
        if ((s >> C[a]) & 1) and ((s >> C[b]) & 1):
            U[s].merge(a, b)
    for i in range(10):
        if (s >> i) & 1:
            cost[s] += W[i]
          
for i in range(K):
    cost[i] = (cost[i], i)
cost.sort()

Q = int(input())
inf = 10 ** 18  
for _ in range(Q):
    ans = inf
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    if not U[-1].same(u, v):
        print(-1)
        continue
    
    for c, i in cost:
        if U[i].same(u, v):
            ans = min(ans, c)
            break
         
    print(ans) if ans != inf else print(-1)
0