結果

問題 No.748 yuki国のお財布事情
ユーザー 2nanoda series2nanoda series
提出日時 2024-03-17 04:09:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 508 ms / 2,000 ms
コード長 3,079 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 93,192 KB
最終ジャッジ日時 2024-03-17 04:09:08
合計ジャッジ時間 8,065 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,612 KB
testcase_01 AC 42 ms
55,612 KB
testcase_02 AC 43 ms
55,612 KB
testcase_03 AC 43 ms
55,612 KB
testcase_04 AC 43 ms
55,612 KB
testcase_05 AC 43 ms
55,612 KB
testcase_06 AC 44 ms
55,612 KB
testcase_07 AC 45 ms
55,612 KB
testcase_08 AC 45 ms
55,612 KB
testcase_09 AC 47 ms
55,612 KB
testcase_10 AC 45 ms
55,612 KB
testcase_11 AC 61 ms
55,612 KB
testcase_12 AC 44 ms
55,612 KB
testcase_13 AC 136 ms
78,472 KB
testcase_14 AC 215 ms
80,276 KB
testcase_15 AC 153 ms
78,984 KB
testcase_16 AC 277 ms
82,904 KB
testcase_17 AC 482 ms
89,772 KB
testcase_18 AC 500 ms
91,068 KB
testcase_19 AC 404 ms
90,076 KB
testcase_20 AC 350 ms
88,984 KB
testcase_21 AC 453 ms
90,120 KB
testcase_22 AC 43 ms
55,612 KB
testcase_23 AC 44 ms
55,612 KB
testcase_24 AC 43 ms
55,612 KB
testcase_25 AC 285 ms
88,224 KB
testcase_26 AC 508 ms
93,192 KB
testcase_27 AC 435 ms
89,856 KB
testcase_28 AC 204 ms
85,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
class UnionFind(): # 要素の番号は 0,1,...,n-1で指定すること。
    def __init__(self,n): # n 要素数 
        self.n=n
        self.group_n=n # 一貫性を持たせるよう注意せよ
        self.parents=[-1]*n  # 各要素に対する親ノードの参照。
        #  ただし、自身がルートの時、集合のサイズを-sizeで持つ。つまり、最初は全て要素1より-1
    def find(self,x):# ルートの要素を探す # 計算コストは O(木の深さ)
        if self.parents[x]<0:
            return x
        else:
            # print(x,end="")
            self.parents[x]=self.find(self.parents[x]) # 親がいるなら親を探す。同時に親を更新する(ルートに直付けする)
            return self.parents[x]
    def union(self,x,y): # x,yを同じ要素にする 。# 計算コストは O(木の深さ)
        xroot=self.find(x)
        yroot=self.find(y)
        if xroot==yroot: # 同じなら何もしない
            return
        self.group_n-=1
        if self.parents[xroot]>self.parents[yroot]: #サイズ比較
            xroot,yroot=yroot,xroot
        # サイズを比較し、大きい方をxrootとする。
        self.parents[xroot]+=self.parents[yroot] # xがさらに大きな木のルートとなり、サイズを更新
        self.parents[yroot] = xroot # サイズの小さいyのrootノードを、xのrootノードの子にする。
    def same(self, x, y): # 計算コストは O(木の深さ)
        return self.find(x) == self.find(y)
    def size(self,x): # 計算コストはO(1)
        return -self.parents[self.find(x)]
    def roots(self):# 計算コストは O(n)  # O(1)に改善可能
        return [i for i, x in enumerate(self.parents) if x < 0] 
    def group_count(self): # 改善したものを使えば計算コストはO(1)
        # return len(self.roots()) # 計算コストは O(n)
        return self.group_n # 計算コストはO(1)
    def members(self, x): # xの属するものを全て見つける。計算コストはO(n) # O(1)に改善可能
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    def all_group_members(self): # 計算コストはO(n)
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
    def __str__(self): # 計算コストはO(n)
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n,m,k=map(int,input().split())

abc=[]
for _ in range(m):
    ai,bi,ci=map(int,input().split())
    ai-=1
    bi-=1
    abc.append([ai,bi,ci])
for _ in range(k):
    ei=int(input())-1
    abc[ei][2]=0

abc.sort(key=lambda x:x[2])
total=sum([el[2] for el in abc])
# print(abc)

# コストが小さい方から考える。
# 繋がっていないなら繋げる
# ノード数は10^5
uf=UnionFind(n)
cost=0
for ai,bi,ci in abc:
    if not uf.same(ai,bi):
        cost+=ci
        uf.union(ai,bi)
print(total-cost)

0