結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー nikoro256nikoro256
提出日時 2024-02-16 22:30:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 931 ms / 2,500 ms
コード長 2,134 bytes
コンパイル時間 419 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 81,448 KB
最終ジャッジ日時 2024-02-16 22:30:41
合計ジャッジ時間 14,851 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 517 ms
79,488 KB
testcase_01 AC 819 ms
79,672 KB
testcase_02 AC 743 ms
80,024 KB
testcase_03 AC 79 ms
76,064 KB
testcase_04 AC 58 ms
64,172 KB
testcase_05 AC 86 ms
76,464 KB
testcase_06 AC 353 ms
77,832 KB
testcase_07 AC 543 ms
78,672 KB
testcase_08 AC 522 ms
78,300 KB
testcase_09 AC 561 ms
78,672 KB
testcase_10 AC 923 ms
81,448 KB
testcase_11 AC 878 ms
80,952 KB
testcase_12 AC 918 ms
81,196 KB
testcase_13 AC 931 ms
81,192 KB
testcase_14 AC 927 ms
81,448 KB
testcase_15 AC 718 ms
78,672 KB
testcase_16 AC 711 ms
78,772 KB
testcase_17 AC 704 ms
78,680 KB
testcase_18 AC 388 ms
77,832 KB
testcase_19 AC 476 ms
78,080 KB
testcase_20 AC 465 ms
78,048 KB
testcase_21 AC 538 ms
78,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


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 = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
    

import sys
input=sys.stdin.readline
N,M=map(int,input().split())
exp2=[2**i for i in range(20)]
edges=[]
for _ in range(M):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edges.append((a,b))
C=list(map(int,input().split()))
W=list(map(int,input().split()))
point=[0]*(2**10)
for i in range(2**10):
    for j in range(10):
        if i&exp2[j]:
            point[i]+=W[j]
Q=int(input())
fa=[10**10]*Q
querys=[]
for _ in range(Q):
    u,v=map(int,input().split())
    u-=1
    v-=1
    querys.append((u,v))
for i in range(2**10):
    uf=UnionFind(N)
    for a,b in edges:
        if exp2[C[a]-1]&i and exp2[C[b]-1]&i:
            uf.union(a,b)
    for j in range(Q):
        u,v=querys[j]
        if uf.same(u,v):
            fa[j]=min(point[i],fa[j])
for i in range(Q):
    if fa[i]==10**10:
        fa[i]=-1
print(*fa,sep='\n')
0