結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー flygonflygon
提出日時 2024-02-16 22:37:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 950 ms / 2,500 ms
コード長 1,880 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 83,428 KB
最終ジャッジ日時 2024-02-16 22:37:47
合計ジャッジ時間 15,024 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 497 ms
80,520 KB
testcase_01 AC 796 ms
81,380 KB
testcase_02 AC 697 ms
81,508 KB
testcase_03 AC 84 ms
76,740 KB
testcase_04 AC 59 ms
66,552 KB
testcase_05 AC 86 ms
76,332 KB
testcase_06 AC 466 ms
79,912 KB
testcase_07 AC 720 ms
82,532 KB
testcase_08 AC 714 ms
81,252 KB
testcase_09 AC 709 ms
82,148 KB
testcase_10 AC 925 ms
83,428 KB
testcase_11 AC 920 ms
83,300 KB
testcase_12 AC 941 ms
83,172 KB
testcase_13 AC 897 ms
83,300 KB
testcase_14 AC 950 ms
83,172 KB
testcase_15 AC 608 ms
80,740 KB
testcase_16 AC 675 ms
80,740 KB
testcase_17 AC 588 ms
80,740 KB
testcase_18 AC 404 ms
79,136 KB
testcase_19 AC 446 ms
79,972 KB
testcase_20 AC 436 ms
79,716 KB
testcase_21 AC 464 ms
80,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd
from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [-1] * (n+1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.p[x] > self.p[y]:
            x, y = y, x
        self.p[x] += self.p[y]
        self.p[y] = x

    def same(self, a, b):
        return self.find(a) == self.find(b)

    def group(self):
        d = defaultdict(list)
        for i in range(1, self.n+1):
            par = self.find(i)
            d[par].append(i)
        return d

n,m = map(int,input().split())
colpair = [[[] for i in range(10)] for i in range(10)]
e = [list(map(int,input().split())) for i in range(m)]

c = list(map(int,input().split()))
w = list(map(int,input().split()))
c = [i-1 for i in c]
for i in range(m):
    u, v= e[i]
    colpair[c[u-1]][c[v-1]].append(i)
q = int(input())
qs = [list(map(int,input().split())) for i in range(q)]
ans = [10**15]*q
for bit in range(1<<10):
    use = []
    cost = 0
    uf = UnionFind(n)
    for i in range(10):
        if (bit>>i) & 1:
            use.append(i)
            cost += w[i]
    
    for c1 in use:
        for c2 in use:
            for i in colpair[c1][c2]:
                u,v = e[i]
                uf.union(u,v)
    
    for i in range(q):
        u,v = qs[i]
        if uf.same(u,v):
            ans[i] = min(ans[i], cost)

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