結果

問題 No.3087 University Coloring
ユーザー titia
提出日時 2025-04-22 19:42:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 619 ms / 2,000 ms
コード長 1,163 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 82,820 KB
実行使用メモリ 131,412 KB
最終ジャッジ日時 2025-04-22 19:42:44
合計ジャッジ時間 21,242 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from heapq import heappop,heappush

N,M=map(int,input().split())

E=[[] for i in range(N)]

for i in range(M):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    E[a].append((b,c))
    E[b].append((a,c))

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

ANS=0
H=[]
USE=[0]*N

USE[0]=1
for to,w in E[0]:
    heappush(H,(-w,to))

while H:
    #print(H)
    while H and USE[H[0][1]]==1:
        heappop(H)

    #print(H)

    if H:
        w,to=heappop(H)
        ANS-=w

        USE[to]=1
        for toto,w in E[to]:
            if USE[toto]==0:
                heappush(H,(-w,toto))
    else:
        break

print(ANS*2)
0