結果

問題 No.3087 University Coloring
ユーザー nasutarou1341
提出日時 2025-04-04 21:56:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 867 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 101,148 KB
最終ジャッジ日時 2025-04-04 21:56:42
合計ジャッジ時間 17,794 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union:
  def __init__(s, num):
    s.T = [-1] * num

  def root(s, x):
    if s.T[x] < 0: return x
    t = s.root(s.T[x])
    s.T[x] = t
    return t

  def merge(s, x, y):
    an, bn = s.root(x), s.root(y)
    if an == bn: return False
    am, bm = -s.T[an], -s.T[bn]
    if am < bm: an, bn = bn, an
    s.T[an] += s.T[bn]
    s.T[bn] = an
    return True

  def size(s, x):
    n = s.root(x)
    return -s.T[n]

  def same(s, x, y):
    return s.root(x) == s.root(y)

  def print(s): #デバッグ用
    print(*s.T)
    ans = []
    for i in range(len(s.T)):
      ans.append(s.root(i))
    print(*ans)

N, M = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(M)]

uni = Union(N)
abc.sort(key = lambda x: x[2], reverse=True)
ans = 0
for a, b, c in abc:
  a -= 1
  b -= 1
  if uni.merge(a, b):
    ans += 2 * c

print(ans)
0