結果

問題 No.1565 Union
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-06-26 13:12:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 447 ms / 2,000 ms
コード長 930 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 102,884 KB
最終ジャッジ日時 2023-12-30 02:21:20
合計ジャッジ時間 7,770 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,608 KB
testcase_01 AC 43 ms
55,608 KB
testcase_02 AC 44 ms
55,608 KB
testcase_03 AC 44 ms
55,608 KB
testcase_04 AC 44 ms
55,608 KB
testcase_05 AC 43 ms
55,608 KB
testcase_06 AC 44 ms
55,608 KB
testcase_07 AC 43 ms
55,608 KB
testcase_08 AC 43 ms
55,608 KB
testcase_09 AC 44 ms
55,608 KB
testcase_10 AC 132 ms
80,344 KB
testcase_11 AC 248 ms
94,816 KB
testcase_12 AC 226 ms
85,412 KB
testcase_13 AC 124 ms
80,244 KB
testcase_14 AC 302 ms
91,388 KB
testcase_15 AC 447 ms
101,608 KB
testcase_16 AC 318 ms
101,352 KB
testcase_17 AC 437 ms
101,608 KB
testcase_18 AC 412 ms
101,608 KB
testcase_19 AC 417 ms
101,736 KB
testcase_20 AC 170 ms
102,884 KB
testcase_21 AC 173 ms
102,884 KB
testcase_22 AC 175 ms
102,884 KB
testcase_23 AC 176 ms
102,884 KB
testcase_24 AC 175 ms
102,884 KB
testcase_25 AC 195 ms
102,884 KB
testcase_26 AC 199 ms
102,884 KB
testcase_27 AC 192 ms
102,884 KB
testcase_28 AC 193 ms
102,884 KB
testcase_29 AC 187 ms
102,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

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