結果

問題 No.1865 Make Cycle
ユーザー 👑 obakyanobakyan
提出日時 2022-04-07 00:25:05
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 1,000 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 18,432 KB
最終ジャッジ日時 2024-05-05 20:22:08
合計ジャッジ時間 4,040 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 54 ms
11,264 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 74 ms
16,896 KB
testcase_05 AC 61 ms
15,488 KB
testcase_06 AC 60 ms
14,208 KB
testcase_07 AC 72 ms
13,696 KB
testcase_08 WA -
testcase_09 AC 58 ms
15,360 KB
testcase_10 AC 86 ms
16,896 KB
testcase_11 AC 101 ms
16,256 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 52 ms
11,776 KB
testcase_15 WA -
testcase_16 AC 79 ms
16,768 KB
testcase_17 AC 53 ms
12,032 KB
testcase_18 AC 60 ms
15,872 KB
testcase_19 AC 99 ms
18,432 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
  if parent[b][a] then
    parent[b][a] = parent[b][a] + 1
  else
    parent[b][a] = 1
  end
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 p, c in pairs(parent[src]) do
      ccnt[p] = ccnt[p] - c
      if ccnt[p] == 0 then
        table.insert(tasks, p)
      end
    end
  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]
  parent[b][a] = parent[b][a] - 1
  if parent[b][a] == 0 then parent[b][a] = nil end
  ccnt[a] = ccnt[a] - 1
  if ccnt[a] == 0 then
    table.insert(tasks, a)
    rm()
  end
  if done == n then print(iq) break end
end
0