結果

問題 No.1565 Union
コンテスト
ユーザー tobusakana
提出日時 2022-12-17 19:41:21
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 300 ms / 2,000 ms
+ 148µs
コード長 569 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 63 ms
コンパイル使用メモリ 81,144 KB
実行使用メモリ 110,820 KB
最終ジャッジ日時 2026-10-05 03:15:17
合計ジャッジ時間 8,831 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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