結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ntudantuda
提出日時 2024-02-17 20:50:16
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,572 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 70,452 KB
最終ジャッジ日時 2024-02-17 20:50:20
合計ジャッジ時間 3,205 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

from atcoder.dsu import DSU
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()))
SW = sum(W)
X = [0] * 1024
dsu = [DSU(N) for _ in range(2**10)]
for i in range(1,1024):
    x = i
    cs = set()
    tmp = 0
    for j in range(10):
        if x & 1:
            cs.add(j)
            tmp += W[j]
        x >>= 1
    X[i] = tmp
    for a,b in AB:
        a -= 1
        b -= 1
        if C[a]-1 in cs and C[b]-1 in cs:
            dsu[i].merge(a,b)
for _ in range(int(input())):
    u,v = map(int,input().split())
    ans = SW + 1
    for i in range(1, 1024):
        if dsu[i].same(u-1,v-1):
            ans = min(ans,X[i])
    if ans == SW + 1:
        print(-1)
    else:
        print(ans)

0