結果

問題 No.1565 Union
ユーザー qibqib
提出日時 2022-11-24 01:08:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 513 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 102,016 KB
最終ジャッジ日時 2024-05-01 01:05:22
合計ジャッジ時間 8,875 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,144 KB
testcase_01 AC 42 ms
54,016 KB
testcase_02 AC 40 ms
53,888 KB
testcase_03 AC 42 ms
54,144 KB
testcase_04 AC 44 ms
54,144 KB
testcase_05 AC 40 ms
53,632 KB
testcase_06 AC 42 ms
55,008 KB
testcase_07 AC 43 ms
54,144 KB
testcase_08 AC 41 ms
54,272 KB
testcase_09 AC 43 ms
54,272 KB
testcase_10 AC 168 ms
81,328 KB
testcase_11 AC 275 ms
94,080 KB
testcase_12 AC 257 ms
86,016 KB
testcase_13 AC 142 ms
81,068 KB
testcase_14 AC 322 ms
90,792 KB
testcase_15 AC 474 ms
102,016 KB
testcase_16 AC 352 ms
99,072 KB
testcase_17 AC 455 ms
99,128 KB
testcase_18 AC 432 ms
99,200 KB
testcase_19 AC 459 ms
99,256 KB
testcase_20 AC 225 ms
99,884 KB
testcase_21 AC 228 ms
99,968 KB
testcase_22 AC 228 ms
99,796 KB
testcase_23 AC 226 ms
100,108 KB
testcase_24 AC 227 ms
99,964 KB
testcase_25 AC 249 ms
99,968 KB
testcase_26 AC 238 ms
99,968 KB
testcase_27 AC 239 ms
99,760 KB
testcase_28 AC 242 ms
99,968 KB
testcase_29 AC 237 ms
100,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n, m = map(int, input().split())

g = [[] for _ in range(n)]
for _ in range(m):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  g[a].append(b)
  g[b].append(a)

src = 0
dst = n - 1
dist = [None for _ in range(n)]
dist[src] = 0
dq = deque()
dq.appendleft(src)
while len(dq) > 0:
  cur = dq.pop()
  for nxt in g[cur]:
    if not dist[nxt] is None:
      continue
    dist[nxt] = dist[cur] + 1
    dq.appendleft(nxt)

if dist[dst] is None:
  print("-1")
else:
  print(dist[dst])
0