結果

問題 No.1565 Union
ユーザー nasubi24nasubi24
提出日時 2021-06-26 13:27:00
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 453 ms / 2,000 ms
コード長 459 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 99,384 KB
最終ジャッジ日時 2023-12-26 06:35:30
合計ジャッジ時間 8,034 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,332 KB
testcase_01 AC 40 ms
53,332 KB
testcase_02 AC 41 ms
55,484 KB
testcase_03 AC 40 ms
55,484 KB
testcase_04 AC 40 ms
53,332 KB
testcase_05 AC 41 ms
55,484 KB
testcase_06 AC 41 ms
55,484 KB
testcase_07 AC 41 ms
55,484 KB
testcase_08 AC 40 ms
53,332 KB
testcase_09 AC 42 ms
55,484 KB
testcase_10 AC 164 ms
80,652 KB
testcase_11 AC 272 ms
93,248 KB
testcase_12 AC 251 ms
85,320 KB
testcase_13 AC 138 ms
80,548 KB
testcase_14 AC 331 ms
90,108 KB
testcase_15 AC 453 ms
98,488 KB
testcase_16 AC 351 ms
98,232 KB
testcase_17 AC 448 ms
98,488 KB
testcase_18 AC 427 ms
98,488 KB
testcase_19 AC 447 ms
98,616 KB
testcase_20 AC 223 ms
99,384 KB
testcase_21 AC 221 ms
99,384 KB
testcase_22 AC 225 ms
99,384 KB
testcase_23 AC 225 ms
99,384 KB
testcase_24 AC 228 ms
99,384 KB
testcase_25 AC 246 ms
99,384 KB
testcase_26 AC 240 ms
99,384 KB
testcase_27 AC 239 ms
99,384 KB
testcase_28 AC 238 ms
99,384 KB
testcase_29 AC 234 ms
99,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
n,m=map(int,input().split())
path=[[] for _ in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    path[a-1].append(b-1)
    path[b-1].append(a-1)
ans=[10**9]*n
ans[0]=0
queue=deque()
queue.append(0)
while len(queue)!=0:
    num=queue.popleft()
    for i in path[num]:
        if ans[i]>ans[num]+1:
            ans[i]=ans[num]+1
            queue.append(i)
if ans[-1]==10**9:
    print(-1)
else:
    print(ans[-1])
0