結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー PNJPNJ
提出日時 2024-02-16 21:44:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,423 ms / 2,500 ms
コード長 2,004 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 242,604 KB
最終ジャッジ日時 2024-02-16 21:44:59
合計ジャッジ時間 32,718 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 897 ms
117,316 KB
testcase_01 AC 1,677 ms
161,504 KB
testcase_02 AC 2,057 ms
226,484 KB
testcase_03 AC 73 ms
73,248 KB
testcase_04 AC 56 ms
64,172 KB
testcase_05 AC 82 ms
76,324 KB
testcase_06 AC 878 ms
97,568 KB
testcase_07 AC 1,804 ms
236,560 KB
testcase_08 AC 1,677 ms
218,512 KB
testcase_09 AC 1,789 ms
238,864 KB
testcase_10 AC 2,307 ms
242,212 KB
testcase_11 AC 2,333 ms
242,192 KB
testcase_12 AC 2,327 ms
242,604 KB
testcase_13 AC 2,373 ms
242,124 KB
testcase_14 AC 2,423 ms
242,400 KB
testcase_15 AC 782 ms
81,804 KB
testcase_16 AC 732 ms
81,172 KB
testcase_17 AC 814 ms
81,556 KB
testcase_18 AC 1,140 ms
123,792 KB
testcase_19 AC 1,342 ms
201,104 KB
testcase_20 AC 1,284 ms
178,960 KB
testcase_21 AC 1,521 ms
238,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

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

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

        if(x == y):
            return
        elif(self.rank[x] > self.rank[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rank[x] == self.rank[y]):
                self.rank[y] += 1

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

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

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

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

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

N,M = map(int,input().split())
G = [[] for i in range(N)]
E = []
for i in range(M):
  A,B = map(int,input().split())
  A -= 1
  B -= 1
  G[A].append(B)
  G[B].append(A)
  E.append((A,B))
C = list(map(int,input().split()))
for i in range(N):
  C[i] -= 1
W = list(map(int,input().split()))
# bit
CC = [0 for i in range(1<<10)]
for i in range(1<<10):
  for j in range(10):
    if (i >> j) & 1:
      CC[i] += W[j]

UF = [UnionFind(N) for i in range(1<<10)]
for i in range(1<<10):
  for u,v in E:
    if (i >> C[u]) & 1 and (i >> C[v]) & 1:
      UF[i].unite(u,v)

inf = 10**12
Q = int(input())
for _ in range(Q):
  u,v = map(int,input().split())
  u -= 1
  v -= 1
  ans = inf
  for bit in range(1<<10):
    if UF[bit].same(u,v):
      ans = min(ans,CC[bit])
  if ans == inf:
    ans = -1
  print(ans)
0