結果

問題 No.1473 おでぶなおばけさん
ユーザー 👑 H20H20
提出日時 2021-04-09 23:03:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 718 ms / 2,000 ms
コード長 2,138 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 87,060 KB
実行使用メモリ 135,108 KB
最終ジャッジ日時 2023-09-07 12:45:32
合計ジャッジ時間 23,591 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,464 KB
testcase_01 AC 91 ms
71,484 KB
testcase_02 AC 682 ms
125,748 KB
testcase_03 AC 568 ms
118,592 KB
testcase_04 AC 444 ms
104,244 KB
testcase_05 AC 275 ms
84,448 KB
testcase_06 AC 565 ms
116,832 KB
testcase_07 AC 707 ms
132,220 KB
testcase_08 AC 718 ms
132,680 KB
testcase_09 AC 665 ms
128,392 KB
testcase_10 AC 324 ms
99,668 KB
testcase_11 AC 328 ms
99,404 KB
testcase_12 AC 310 ms
96,932 KB
testcase_13 AC 234 ms
90,484 KB
testcase_14 AC 205 ms
85,756 KB
testcase_15 AC 290 ms
95,960 KB
testcase_16 AC 290 ms
95,384 KB
testcase_17 AC 129 ms
78,216 KB
testcase_18 AC 141 ms
80,280 KB
testcase_19 AC 401 ms
95,748 KB
testcase_20 AC 554 ms
118,704 KB
testcase_21 AC 563 ms
107,348 KB
testcase_22 AC 505 ms
118,336 KB
testcase_23 AC 451 ms
113,016 KB
testcase_24 AC 457 ms
112,260 KB
testcase_25 AC 490 ms
114,512 KB
testcase_26 AC 474 ms
116,388 KB
testcase_27 AC 261 ms
88,656 KB
testcase_28 AC 625 ms
135,108 KB
testcase_29 AC 444 ms
107,040 KB
testcase_30 AC 510 ms
116,016 KB
testcase_31 AC 636 ms
129,024 KB
testcase_32 AC 527 ms
118,920 KB
testcase_33 AC 515 ms
111,740 KB
testcase_34 AC 372 ms
97,096 KB
testcase_35 AC 338 ms
96,812 KB
testcase_36 AC 358 ms
101,372 KB
testcase_37 AC 439 ms
112,312 KB
testcase_38 AC 201 ms
83,060 KB
testcase_39 AC 301 ms
96,008 KB
testcase_40 AC 291 ms
95,960 KB
testcase_41 AC 291 ms
100,232 KB
testcase_42 AC 295 ms
100,100 KB
testcase_43 AC 276 ms
105,476 KB
testcase_44 AC 283 ms
105,608 KB
testcase_45 AC 280 ms
105,488 KB
testcase_46 AC 617 ms
111,108 KB
testcase_47 AC 703 ms
116,812 KB
testcase_48 AC 582 ms
115,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict
from collections import deque

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):
        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,M = map(int, input().split())
Q = [[0] * 3 for i in range(M)] 
L = []
for i in range(N):
    L.append([])


for i in range(M):
    u,v,d = map(int, input().split())
    Q[i][0]=u-1
    Q[i][1]=v-1
    Q[i][2]=d
    L[u-1].append((v-1,d))
    L[v-1].append((u-1,d))

Q = sorted(Q, key=lambda x: x[2],reverse=True)
W = 0
uf = UnionFind(N)
for u,v,d in Q:
    uf.union(u,v)
    if uf.same(0,N-1):
        W=d
        break


q = deque()
qq = deque()
q.append(0)
qq.append(0)
ALL = [0]*N
while True:
    temp = q.popleft()
    tempq = qq.popleft()
    if temp==N-1:
        print(W,tempq)
        exit()
    for t,d in L[temp]:
        if ALL[t]==0 and W<=d:
            ALL[t]=1
            q.append(t)
            qq.append(tempq+1)
0