結果

問題 No.1565 Union
ユーザー tobusakana
提出日時 2022-12-17 19:41:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 569 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 102,428 KB
最終ジャッジ日時 2024-12-20 09:05:13
合計ジャッジ時間 8,956 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
G = [[] for i in range(N)]
for _ in range(M):
    a,b = map(int,input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
    
from collections import deque
q = deque([])
q.append([0, -1, 0])
visited = [False] * N
while q:
    v,p,c = q.popleft()
    if visited[v]:
        continue
    visited[v] = True
    if v == N - 1:
        print(c)
        break
    for child in G[v]:
        if child == p:
            continue
        if visited[child]:
            continue
        q.append([child, v, c + 1])
else:
    print(-1)
0