結果

問題 No.1565 Union
ユーザー aaaaaaaaaa2230
提出日時 2021-06-26 14:53:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 438 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 100,760 KB
最終ジャッジ日時 2024-12-20 17:06:14
合計ジャッジ時間 9,631 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n,m = map(int,input().split())
e = [[] for i in range(n)]
for _ in range(m):
    a,b = [int(x)-1 for x in input().split()]
    e[a].append(b)
    e[b].append(a)

dis = [n+1]*n
dis[0] = 0
q = deque([0])
while q:
    now = q.popleft()
    for nex in e[now]:
        if dis[nex] > dis[now]+1:
            q.append(nex)
            dis[nex] = dis[now]+1
if dis[-1] == n+1:
    print(-1)
else:
    print(dis[-1])
0