結果

問題 No.1639 最小通信路
ユーザー とりゐとりゐ
提出日時 2021-08-07 03:29:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 2,320 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 81,608 KB
実行使用メモリ 76,260 KB
最終ジャッジ日時 2023-10-17 08:49:04
合計ジャッジ時間 4,764 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,656 KB
testcase_01 AC 44 ms
55,656 KB
testcase_02 AC 51 ms
61,088 KB
testcase_03 AC 47 ms
57,704 KB
testcase_04 AC 92 ms
76,260 KB
testcase_05 AC 45 ms
55,656 KB
testcase_06 AC 45 ms
55,656 KB
testcase_07 AC 44 ms
55,656 KB
testcase_08 AC 48 ms
57,704 KB
testcase_09 AC 44 ms
55,656 KB
testcase_10 AC 45 ms
55,656 KB
testcase_11 AC 45 ms
55,656 KB
testcase_12 AC 45 ms
55,656 KB
testcase_13 AC 46 ms
55,656 KB
testcase_14 AC 44 ms
55,656 KB
testcase_15 AC 44 ms
55,656 KB
testcase_16 AC 46 ms
55,656 KB
testcase_17 AC 47 ms
55,656 KB
testcase_18 AC 44 ms
55,656 KB
testcase_19 AC 45 ms
55,656 KB
testcase_20 AC 51 ms
61,088 KB
testcase_21 AC 43 ms
55,656 KB
testcase_22 AC 45 ms
55,656 KB
testcase_23 AC 45 ms
55,656 KB
testcase_24 AC 44 ms
55,656 KB
testcase_25 AC 44 ms
55,656 KB
testcase_26 AC 43 ms
55,656 KB
testcase_27 AC 43 ms
55,656 KB
testcase_28 AC 43 ms
55,656 KB
testcase_29 AC 44 ms
55,656 KB
testcase_30 AC 46 ms
55,656 KB
testcase_31 AC 44 ms
55,656 KB
testcase_32 AC 45 ms
55,656 KB
testcase_33 AC 44 ms
55,656 KB
testcase_34 AC 45 ms
55,656 KB
testcase_35 AC 53 ms
63,352 KB
testcase_36 AC 48 ms
57,704 KB
testcase_37 AC 45 ms
55,656 KB
testcase_38 AC 43 ms
55,656 KB
testcase_39 AC 44 ms
55,656 KB
testcase_40 AC 43 ms
55,656 KB
testcase_41 AC 44 ms
55,656 KB
testcase_42 AC 47 ms
55,656 KB
testcase_43 AC 44 ms
55,656 KB
testcase_44 AC 44 ms
55,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
from collections import deque,Counter
from sys import stdin

#sys.setrecursionlimit(10**7)
int1=lambda x: int(x)-1

inp=lambda :int(input())
mi=lambda :map(int,input().split())
li=lambda :list(mi())
mi1=lambda :map(int1,input().split())
li1=lambda :list(mi1())
mis=lambda :map(str,input().split())
lis=lambda :list(mis())

stinput=lambda :stdin.readline()[:-1]
stinp=lambda :int(stinput())
stmi=lambda :map(int, stdin.readline().split())
stli=lambda :list(stmi())
stmi1=lambda :map(int1, stdin.readline().split())
stli1=lambda :list(stmi1())
stmis=lambda :stdin.readline()[:-1]

pr=print

from collections import defaultdict
"""
#初期値 0
d=defaultdict(int)

#初期値 1
d=defaultdict(lambda:1)
"""

mod=10**9+7
Mod=998244353
INF=10**18
ans=0

#UnionFind
from collections import defaultdict

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 union(self, 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 size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        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):
        # {0: [0, 2], 1: [1, 3, 4, 5]}
        # 辞書で返す
        # d=uf.all_group_members() で使う
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n=inp()
uf=UnionFind(n)
for i in range(10**5):
  a,b,c=mi()
  a-=1
  b-=1
  uf.union(a,b)
  if uf.size(0)==n:
    print(c)
    exit()
0