結果

問題 No.1473 おでぶなおばけさん
ユーザー 👑 rin204rin204
提出日時 2022-01-18 04:14:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 670 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 115,012 KB
最終ジャッジ日時 2024-11-23 13:14:13
合計ジャッジ時間 21,259 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
54,144 KB
testcase_01 AC 48 ms
54,016 KB
testcase_02 AC 609 ms
110,408 KB
testcase_03 AC 497 ms
106,748 KB
testcase_04 AC 377 ms
95,488 KB
testcase_05 AC 224 ms
80,560 KB
testcase_06 AC 490 ms
105,492 KB
testcase_07 AC 644 ms
113,880 KB
testcase_08 AC 670 ms
114,888 KB
testcase_09 AC 586 ms
115,012 KB
testcase_10 AC 277 ms
87,168 KB
testcase_11 AC 268 ms
86,784 KB
testcase_12 AC 275 ms
86,800 KB
testcase_13 AC 196 ms
83,012 KB
testcase_14 AC 165 ms
81,024 KB
testcase_15 AC 262 ms
87,876 KB
testcase_16 AC 265 ms
86,784 KB
testcase_17 AC 86 ms
77,184 KB
testcase_18 AC 94 ms
77,184 KB
testcase_19 AC 347 ms
89,100 KB
testcase_20 AC 471 ms
103,612 KB
testcase_21 AC 474 ms
95,008 KB
testcase_22 AC 430 ms
105,756 KB
testcase_23 AC 393 ms
103,268 KB
testcase_24 AC 389 ms
100,200 KB
testcase_25 AC 412 ms
104,640 KB
testcase_26 AC 409 ms
104,408 KB
testcase_27 AC 202 ms
84,096 KB
testcase_28 AC 555 ms
112,088 KB
testcase_29 AC 360 ms
96,440 KB
testcase_30 AC 420 ms
101,392 KB
testcase_31 AC 496 ms
113,040 KB
testcase_32 AC 389 ms
107,036 KB
testcase_33 AC 440 ms
102,336 KB
testcase_34 AC 329 ms
90,516 KB
testcase_35 AC 276 ms
88,064 KB
testcase_36 AC 305 ms
91,632 KB
testcase_37 AC 402 ms
101,736 KB
testcase_38 AC 157 ms
80,452 KB
testcase_39 AC 268 ms
87,040 KB
testcase_40 AC 270 ms
87,040 KB
testcase_41 AC 268 ms
86,912 KB
testcase_42 AC 266 ms
86,912 KB
testcase_43 AC 228 ms
93,440 KB
testcase_44 AC 238 ms
93,436 KB
testcase_45 AC 235 ms
93,312 KB
testcase_46 AC 562 ms
102,940 KB
testcase_47 AC 653 ms
107,692 KB
testcase_48 AC 523 ms
104,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = 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
        self.group -= 1
        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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
        
n, m = map(int, input().split())
std = [list(map(int, input().split())) for _ in range(m)]
std.sort(key = lambda x:-x[2])
UF = UnionFind(n)
for s, t, d in std:
    s -= 1
    t -= 1
    UF.union(s, t)
    if UF.same(0, n - 1):
        w = d
        break
    
edges = [[] for _ in range(n)]
for s, t, d in std:
    if d < w:
        continue
    s -= 1
    t -= 1
    edges[s].append(t)
    edges[t].append(s)

queue = deque()
dist = [-1] * n
dist[0] = 0
queue.append(0)
while queue:
    pos = queue.popleft()
    for npos in edges[pos]:
        if dist[npos] != -1:
            continue
        dist[npos] = dist[pos] + 1
        queue.append(npos)
print(w, dist[-1])
0