結果

問題 No.748 yuki国のお財布事情
ユーザー hedwig100hedwig100
提出日時 2020-05-05 17:58:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 10,812 KB
実行使用メモリ 29,120 KB
最終ジャッジ日時 2023-09-09 05:43:57
合計ジャッジ時間 4,638 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,044 KB
testcase_01 AC 16 ms
8,140 KB
testcase_02 AC 16 ms
8,104 KB
testcase_03 AC 17 ms
8,100 KB
testcase_04 AC 17 ms
8,104 KB
testcase_05 AC 18 ms
8,060 KB
testcase_06 AC 17 ms
8,032 KB
testcase_07 AC 16 ms
8,096 KB
testcase_08 AC 17 ms
8,032 KB
testcase_09 AC 17 ms
8,152 KB
testcase_10 AC 16 ms
8,064 KB
testcase_11 AC 16 ms
8,228 KB
testcase_12 AC 17 ms
8,096 KB
testcase_13 AC 49 ms
11,140 KB
testcase_14 AC 68 ms
12,684 KB
testcase_15 AC 54 ms
11,440 KB
testcase_16 AC 114 ms
15,916 KB
testcase_17 AC 269 ms
26,780 KB
testcase_18 AC 283 ms
27,516 KB
testcase_19 AC 266 ms
26,952 KB
testcase_20 AC 231 ms
24,696 KB
testcase_21 AC 296 ms
27,724 KB
testcase_22 AC 17 ms
8,032 KB
testcase_23 AC 17 ms
8,160 KB
testcase_24 AC 16 ms
8,032 KB
testcase_25 AC 261 ms
24,100 KB
testcase_26 AC 332 ms
29,120 KB
testcase_27 AC 331 ms
28,600 KB
testcase_28 AC 172 ms
20,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline

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 unite(self,x,y): #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 same(self,x,y):#xとyが同じグループにいるか判定
        return self.find(x) == self.find(y)
    
    def members(self,x):#xと同じグループのメンバーを列挙
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    def size(self,x):#xが属するグループのメンバーの数
        return -self.parents[self.find(x)]
    
    def roots(self):#ufの根を列挙
        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):#根:メンバの辞書を返す
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):#print()での表示用
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    N,M,K = map(int,input().split())
    edge = [tuple(map(int,input().split())) for _ in range(M)]
    uf = UnionFind(N)
    
    ans = 0
    used = [0] * M
    for _ in range(K):
        e = int(input())
        e -= 1
        uf.unite(edge[e][0] - 1,edge[e][1] - 1)
        used[e] = 1
    
    next_edge = [e for i,e in enumerate(edge) if used[i] == 0]
    next_edge.sort(key = lambda x:x[2])
    for a,b,c in next_edge:
        if uf.same(a - 1,b - 1):
            ans += c
        else:
            uf.unite(a - 1,b - 1)
    print(ans)

if __name__ == '__main__':
    main()
0