結果

問題 No.1865 Make Cycle
ユーザー 👑 obakyanobakyan
提出日時 2022-04-07 00:32:35
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 129 ms / 3,000 ms
コード長 955 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 25,216 KB
最終ジャッジ日時 2024-05-05 20:23:35
合計ジャッジ時間 3,693 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
17,664 KB
testcase_01 AC 70 ms
14,848 KB
testcase_02 AC 100 ms
20,224 KB
testcase_03 AC 83 ms
18,048 KB
testcase_04 AC 108 ms
23,296 KB
testcase_05 AC 85 ms
20,992 KB
testcase_06 AC 83 ms
19,200 KB
testcase_07 AC 89 ms
18,176 KB
testcase_08 AC 116 ms
22,272 KB
testcase_09 AC 82 ms
20,864 KB
testcase_10 AC 114 ms
22,016 KB
testcase_11 AC 99 ms
21,376 KB
testcase_12 AC 88 ms
19,712 KB
testcase_13 AC 95 ms
17,408 KB
testcase_14 AC 69 ms
16,128 KB
testcase_15 AC 109 ms
20,736 KB
testcase_16 AC 113 ms
22,656 KB
testcase_17 AC 67 ms
16,128 KB
testcase_18 AC 84 ms
21,376 KB
testcase_19 AC 129 ms
25,216 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, q = io.read("*n", "*n")
local ea, eb = {}, {}
local ccnt, parent = {}, {}
for i = 1, n do
  ccnt[i] = 0
  parent[i] = {}
end
for iq = 1, q do
  local a, b = io.read("*n", "*n")
  ea[iq], eb[iq] = a, b
  ccnt[a] = ccnt[a] + 1
  parent[b][iq] = true
end
local tasks = {}
for i = 1, n do
  if ccnt[i] == 0 then
    table.insert(tasks, i)
  end
end
local done = 0
local function rm()
  while done < #tasks do
    done = done + 1
    local src = tasks[done]
    for pi, _u in pairs(parent[src]) do
      local p = ea[pi]
      ccnt[p] = ccnt[p] - 1
      if ccnt[p] == 0 then
        table.insert(tasks, p)
      end
    end
    parent[src] = {}
  end
end
rm()
if done == n then print(-1) os.exit() end
for iq = q, 1, -1 do
  local a, b = ea[iq], eb[iq]
  if parent[b][iq] then
    parent[b][iq] = nil
    ccnt[a] = ccnt[a] - 1
    if ccnt[a] == 0 then
      table.insert(tasks, a)
      rm()
    end
  end
  if done == n then print(iq) break end
end
0