結果

問題 No.1565 Union
ユーザー SPD_9X2
提出日時 2021-06-26 13:12:41
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 251 ms / 2,000 ms
+ 954µs
コード長 930 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 790 ms
コンパイル使用メモリ 81,592 KB
実行使用メモリ 110,232 KB
最終ジャッジ日時 2026-10-10 07:41:50
合計ジャッジ時間 6,351 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

"""

https://yukicoder.me/problems/no/1565


"""

from sys import stdin
#重みのないグラフでの最短経路問題
#隣接リストと始点を与えると始点からの距離のリスト & 親のリストを返す
from collections import deque
def NC_Dij(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    
    q = deque([start])
    plis = [i for i in range(len(lis))]

    while len(q) > 0:
        now = q.popleft()

        for nex in lis[now]:

            if ret[nex] > ret[now] + 1:
                ret[nex] = ret[now] + 1
                plis[nex] = now
                q.append(nex)

    return ret,plis

N,M = map(int,stdin.readline().split())

lis = [ [] for i in range(N) ]

for i in range(M):

    a,b = map(int,stdin.readline().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)

dlis,tmp = NC_Dij(lis,0)

ans = dlis[-1]

print (ans if ans!=float("inf") else -1)
0