結果

問題 No.748 yuki国のお財布事情
ユーザー rlangevin
提出日時 2023-07-16 13:04:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 526 ms / 2,000 ms
コード長 1,334 bytes
コンパイル時間 1,041 ms
コンパイル使用メモリ 81,864 KB
実行使用メモリ 100,188 KB
最終ジャッジ日時 2024-09-17 12:43:35
合計ジャッジ時間 7,557 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

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

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


N, M, K = map(int, input().split())
Edge = []
ans = 0
for i in range(M):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    Edge.append((c, a, b, i))
    ans += c
    
S = set()
for i in range(K):
    S.add(int(input()) - 1)
    
U = UnionFind(N)
for c, a, b, i in Edge:
    if i in S:
        U.union(a, b)
        ans -= c
        
Edge.sort()
for c, a, b, i in Edge:
    if U.is_same(a, b):
        continue
    ans -= c
    U.union(a, b)
    
print(ans)
0