結果

問題 No.1865 Make Cycle
ユーザー 👑 obakyanobakyan
提出日時 2022-04-07 00:32:35
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 167 ms / 3,000 ms
コード長 955 bytes
コンパイル時間 51 ms
コンパイル使用メモリ 5,384 KB
実行使用メモリ 18,620 KB
最終ジャッジ日時 2023-08-18 15:11:49
合計ジャッジ時間 3,871 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
13,272 KB
testcase_01 AC 77 ms
11,024 KB
testcase_02 AC 131 ms
15,672 KB
testcase_03 AC 105 ms
13,744 KB
testcase_04 AC 134 ms
17,084 KB
testcase_05 AC 108 ms
15,860 KB
testcase_06 AC 108 ms
14,388 KB
testcase_07 AC 112 ms
13,656 KB
testcase_08 AC 145 ms
17,008 KB
testcase_09 AC 96 ms
15,748 KB
testcase_10 AC 141 ms
16,944 KB
testcase_11 AC 117 ms
16,300 KB
testcase_12 AC 110 ms
15,292 KB
testcase_13 AC 120 ms
13,500 KB
testcase_14 AC 86 ms
11,684 KB
testcase_15 AC 133 ms
16,144 KB
testcase_16 AC 136 ms
17,032 KB
testcase_17 AC 83 ms
12,132 KB
testcase_18 AC 99 ms
16,244 KB
testcase_19 AC 167 ms
18,620 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,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