結果

問題 No.1565 Union
ユーザー wgrape
提出日時 2025-03-17 09:56:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 400 ms / 2,000 ms
コード長 509 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 101,880 KB
最終ジャッジ日時 2025-03-17 09:56:21
合計ジャッジ時間 8,796 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, 0])
visited = [False] * N
visited[0] = True
while q:
    v, c = q.popleft()
    if v == N - 1:
        print(c)
        break
    for child in G[v]:
        if visited[child]:
            continue
        visited[child] = True
        q.append([child, c + 1])
else:
    print(-1)
    
0