結果

問題 No.1565 Union
ユーザー 👑 obakyanobakyan
提出日時 2022-02-06 01:24:02
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 528 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 29,952 KB
最終ジャッジ日時 2024-05-04 01:25:15
合計ジャッジ時間 5,026 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 72 ms
6,016 KB
testcase_11 AC 130 ms
23,424 KB
testcase_12 AC 114 ms
9,984 KB
testcase_13 AC 38 ms
8,320 KB
testcase_14 AC 158 ms
18,176 KB
testcase_15 AC 302 ms
29,952 KB
testcase_16 AC 183 ms
27,776 KB
testcase_17 AC 263 ms
29,824 KB
testcase_18 AC 255 ms
29,952 KB
testcase_19 AC 263 ms
29,952 KB
testcase_20 AC 133 ms
29,184 KB
testcase_21 AC 136 ms
29,184 KB
testcase_22 AC 135 ms
29,184 KB
testcase_23 AC 138 ms
29,184 KB
testcase_24 AC 136 ms
29,184 KB
testcase_25 AC 158 ms
29,312 KB
testcase_26 AC 159 ms
29,184 KB
testcase_27 AC 156 ms
29,184 KB
testcase_28 AC 167 ms
29,184 KB
testcase_29 AC 158 ms
29,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, m = io.read("*n", "*n")
local len = {}
local edge = {}
for i = 1, n do
  len[i] = -1
  edge[i] = {}
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
len[1] = 0
local done = 0
while done < #tasks do
  done = done + 1
  local src = tasks[done]
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    if len[dst] < 0 then
      len[dst] = len[src] + 1
      table.insert(tasks, dst)
    end
  end
end
print(0 < len[n] and len[n] or -1)
0