結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー hato336hato336
提出日時 2024-02-16 21:38:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,373 ms / 2,500 ms
コード長 2,390 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 96,020 KB
最終ジャッジ日時 2024-02-16 21:38:49
合計ジャッジ時間 21,288 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 727 ms
92,748 KB
testcase_01 AC 1,050 ms
93,736 KB
testcase_02 AC 1,105 ms
94,352 KB
testcase_03 AC 216 ms
89,440 KB
testcase_04 AC 165 ms
88,800 KB
testcase_05 AC 198 ms
89,952 KB
testcase_06 AC 623 ms
91,616 KB
testcase_07 AC 909 ms
92,768 KB
testcase_08 AC 864 ms
92,512 KB
testcase_09 AC 865 ms
92,768 KB
testcase_10 AC 1,324 ms
95,588 KB
testcase_11 AC 1,350 ms
95,524 KB
testcase_12 AC 1,373 ms
96,020 KB
testcase_13 AC 1,319 ms
95,340 KB
testcase_14 AC 1,323 ms
95,732 KB
testcase_15 AC 870 ms
92,164 KB
testcase_16 AC 889 ms
92,120 KB
testcase_17 AC 905 ms
92,288 KB
testcase_18 AC 638 ms
91,744 KB
testcase_19 AC 800 ms
92,624 KB
testcase_20 AC 773 ms
92,140 KB
testcase_21 AC 871 ms
93,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
input = sys.stdin.readline
#n,m = map(int,input().split())
#for i in range(n):
#    alist.append(list(map(int,input().split())))
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items())
n,m = map(int,input().split())
edge = [[] for i in range(n)]
e = []
for i in range(m):
    u,v = map(int,input().split())
    u-=1
    v-=1
    e.append((u,v))
    edge[u].append(v)
    edge[v].append(u)
color = list(map(lambda x:int(x)-1,input().split()))
w = list(map(int,input().split()))
q = int(input())
query = [list(map(lambda x:int(x)-1,input().split())) for i in range(q)]
ans = [10**18 for i in range(q)]

for i in itertools.product([0,1],repeat=10):
    uf = UnionFind(n)
    cost = sum(w[j] if i[j] == 1 else 0 for j in range(10))
    for u,v in e:
        if i[color[u]] == 1 and i[color[v]] == 1:
            uf.union(u,v)
    for j in range(q):
        x,y = query[j]
        if i[color[x]] == 1 and i[color[y]] == 1 and uf.same(x,y):
            ans[j] = min(ans[j],cost)

for i in ans:
    print(-1 if i == 10**18 else i)
0