結果

問題 No.1565 Union
ユーザー kept1994kept1994
提出日時 2021-08-30 01:39:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 480 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 100,792 KB
最終ジャッジ日時 2023-12-18 03:05:02
合計ジャッジ時間 9,160 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,616 KB
testcase_01 AC 42 ms
55,616 KB
testcase_02 AC 41 ms
55,616 KB
testcase_03 AC 41 ms
55,616 KB
testcase_04 AC 41 ms
55,616 KB
testcase_05 AC 41 ms
55,616 KB
testcase_06 AC 42 ms
55,616 KB
testcase_07 AC 42 ms
55,616 KB
testcase_08 AC 41 ms
55,616 KB
testcase_09 AC 42 ms
55,616 KB
testcase_10 AC 179 ms
80,652 KB
testcase_11 AC 296 ms
93,240 KB
testcase_12 AC 275 ms
85,336 KB
testcase_13 AC 145 ms
80,420 KB
testcase_14 AC 344 ms
90,100 KB
testcase_15 AC 476 ms
99,896 KB
testcase_16 AC 380 ms
99,000 KB
testcase_17 AC 480 ms
100,152 KB
testcase_18 AC 450 ms
99,768 KB
testcase_19 AC 464 ms
99,896 KB
testcase_20 AC 225 ms
99,896 KB
testcase_21 AC 228 ms
100,152 KB
testcase_22 AC 235 ms
100,152 KB
testcase_23 AC 234 ms
100,152 KB
testcase_24 AC 234 ms
100,792 KB
testcase_25 AC 251 ms
100,792 KB
testcase_26 AC 245 ms
100,792 KB
testcase_27 AC 246 ms
100,792 KB
testcase_28 AC 245 ms
100,792 KB
testcase_29 AC 241 ms
100,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys

def main():
    N, M = map(int, input().split())
    INF = 10 ** 16
    from collections import deque
    def bfs(edges: "List[to]", start_node: int) -> list:
        q = deque()
        dist = [INF] * len(edges)
        q.append(start_node)
        dist[start_node] = 0
        while q:
            now = q.popleft()
            for next in edges[now]:
                if dist[next] != INF:
                    continue
                q.append(next)
                dist[next] = dist[now] + 1
        return dist
    G = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(lambda i: int(i) - 1, input().split())
        G[a].append(b)
        G[b].append(a)
    d = bfs(G, 0)
    ans = d[N - 1]
    print(-1 if ans == INF else ans)



if __name__ == '__main__':
    main()
0