結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ShirotsumeShirotsume
提出日時 2024-02-16 23:48:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,138 ms / 2,500 ms
コード長 2,619 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 84,464 KB
最終ジャッジ日時 2024-09-28 22:55:31
合計ジャッジ時間 18,022 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 612 ms
82,524 KB
testcase_01 AC 979 ms
82,632 KB
testcase_02 AC 929 ms
83,484 KB
testcase_03 AC 98 ms
77,524 KB
testcase_04 AC 57 ms
67,528 KB
testcase_05 AC 91 ms
77,696 KB
testcase_06 AC 441 ms
79,916 KB
testcase_07 AC 717 ms
81,724 KB
testcase_08 AC 691 ms
81,120 KB
testcase_09 AC 720 ms
81,496 KB
testcase_10 AC 1,115 ms
84,256 KB
testcase_11 AC 1,123 ms
84,276 KB
testcase_12 AC 1,127 ms
83,872 KB
testcase_13 AC 1,121 ms
84,464 KB
testcase_14 AC 1,138 ms
84,396 KB
testcase_15 AC 841 ms
81,508 KB
testcase_16 AC 833 ms
81,364 KB
testcase_17 AC 865 ms
82,156 KB
testcase_18 AC 477 ms
80,204 KB
testcase_19 AC 637 ms
81,380 KB
testcase_20 AC 555 ms
80,500 KB
testcase_21 AC 693 ms
81,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 61 - 1
mod = 998244353
class dsu():
    n=1
    parent_or_size=[-1 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.num = N
        self.parent_or_size=[-1 for i in range(N)]
    def merge(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        x=self.leader(a)
        y=self.leader(b)
        if x==y:
            return x
        self.num -= 1
        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,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        return self.leader(a)==self.leader(b)
    def leader(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        if (self.parent_or_size[a]<0):
            return a
        self.parent_or_size[a]=self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]
    def size(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        return -self.parent_or_size[self.leader(a)]
    def groups(self):
        leader_buf=[0 for i in range(self.n)]
        group_size=[0 for i in range(self.n)]
        for i in range(self.n):
            leader_buf[i]=self.leader(i)
            group_size[leader_buf[i]]+=1
        result=[[] for i in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2=[]
        for i in range(self.n):
            if len(result[i])>0:
                result2.append(result[i])
        return result2
    def count(self):
        return self.num


n, m = mi()

EDGE = [li() for _ in range(m)]

c = [v - 1 for v in li()]
w = li()
q = ii()
Qu = [li() for _ in range(q)]
ans = [inf] * q
for bit in range(1 << (10)):
    U = dsu(n)
    cost = 0
    for i in range(10):
        if 1 & (bit >> i):
            cost += w[i]
    for u, v in EDGE:
        u -= 1; v -= 1
        if 1 & (bit >> c[u]) and 1 & (bit >> c[v]):
            U.merge(u, v)
    for i in range(q):
        x, y = Qu[i]
        x -= 1; y -= 1
        if U.same(x, y):
            ans[i] = min(ans[i], cost)
for i in range(q):
    if ans[i] == inf:
        ans[i] = -1
    print(ans[i])
0