結果

問題 No.274 The Wall
ユーザー 👑 obakyanobakyan
提出日時 2021-07-22 19:19:48
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 5,107 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 44,916 KB
最終ジャッジ日時 2024-07-17 15:05:21
合計ジャッジ時間 2,696 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 9 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 22 ms
6,940 KB
testcase_12 AC 17 ms
6,944 KB
testcase_13 AC 6 ms
6,944 KB
testcase_14 AC 19 ms
6,944 KB
testcase_15 AC 36 ms
6,944 KB
testcase_16 AC 424 ms
44,916 KB
testcase_17 AC 382 ms
44,136 KB
testcase_18 AC 406 ms
44,312 KB
testcase_19 AC 56 ms
6,940 KB
testcase_20 AC 63 ms
6,944 KB
testcase_21 AC 67 ms
6,940 KB
testcase_22 AC 67 ms
6,944 KB
testcase_23 AC 69 ms
6,940 KB
testcase_24 AC 71 ms
6,940 KB
testcase_25 AC 72 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local SCCD = {}
SCCD.create = function(self, n)
  self.n = n
  self.asked, self.root = {}, {}
  self.edge, self.edge_asked = {}, {}
  self.invedge, self.invedge_asked = {}, {}
  for i = 1, n do
    self.asked[i] = false
    self.root[i] = 0
    self.edge[i] = {}
    self.edge_asked[i] = 0
    self.invedge[i] = {}
    self.invedge_asked[i] = 0
  end
  self.edgeinfo = {}
end
SCCD.addEdge = function(self, src, dst)
  table.insert(self.edge[src], dst)
  table.insert(self.invedge[dst], src)
  table.insert(self.edgeinfo, src)
  table.insert(self.edgeinfo, dst)
end

SCCD.dfs = function(self, spos, dfs_way)
  local asked = self.asked
  local edge, edge_asked = self.edge, self.edge_asked
  local tasks = {spos}
  while 0 < #tasks do
    local src = tasks[#tasks]
    asked[src] = true
    table.remove(tasks)
    if edge_asked[src] == #edge[src] then
      table.insert(dfs_way, src)
    else
      table.insert(tasks, src)
      edge_asked[src] = edge_asked[src] + 1
      local dst = edge[src][edge_asked[src]]
      if not asked[dst] then
        table.insert(tasks, dst)
      end
    end
  end
end

SCCD.invdfs = function(self, spos, rootid)
  local asked, root = self.asked, self.root
  local invedge, invedge_asked = self.invedge, self.invedge_asked
  local tasks = {spos}
  while 0 < #tasks do
    local src = tasks[#tasks]
    root[src] = rootid
    table.remove(tasks)
    while invedge_asked[src] < #invedge[src] do
      invedge_asked[src] = invedge_asked[src] + 1
      local dst = invedge[src][invedge_asked[src]]
      if asked[dst] and root[dst] == 0 then
        table.insert(tasks, src)
        table.insert(tasks, dst)
        break
      end
    end
  end
end

SCCD.categorize = function(self)
  local asked, root = self.asked, self.root
  for src = 1, self.n do
    if not asked[src] then
      local dfs_way = {}
      self:dfs(src, dfs_way)
      for i = #dfs_way, 1, -1 do
        local src = dfs_way[i]
        if root[src] == 0 then
          self:invdfs(src, src)
        end
      end
    end
  end
end


SCCD.make_group_graph = function(self)
  self.gsize = {}
  self.gedge = {}
  self.gpcnt = {}
  self.glen = {}
  self.gmember = {}
  local gsize, gmember, glen = self.gsize, self.gmember, self.glen
  local gedge, gpcnt = self.gedge, self.gpcnt
  local root = self.root
  local edgeinfo = self.edgeinfo
  for i = 1, self.n do
    gsize[i] = 0
    gedge[i], gpcnt[i] = {}, 0
    glen[i] = 0
    gmember[i] = {}
  end
  for i = 1, self.n do
    local r = root[i]
    gsize[r] = gsize[r] + 1
    table.insert(gmember[r], i)
  end
  for i = 1, #edgeinfo, 2 do
    local a, b = edgeinfo[i], edgeinfo[i + 1]
    local ra, rb = root[a], root[b]
    if ra ~= rb then
      table.insert(gedge[ra], rb)
      gpcnt[rb] = gpcnt[rb] + 1
    end
  end
end

SCCD.toposort = function(self)
  self:make_group_graph()
  local gsize, gedge, gpcnt = self.gsize, self.gedge, self.gpcnt
  local topoary = {}
  self.topoary = topoary
  local topo_tasks = {}
  for i = 1, self.n do
    if 0 < gsize[i] and gpcnt[i] == 0 then
      table.insert(topo_tasks, i)
    end
  end
  local topo_done = 0
  while topo_done < #topo_tasks do
    topo_done = topo_done + 1
    local g = topo_tasks[topo_done]
    topoary[topo_done] = g
    for i = 1, #gedge[g] do
      local dst = gedge[g][i]
      gpcnt[dst] = gpcnt[dst] - 1
      if gpcnt[dst] == 0 then
        table.insert(topo_tasks, dst)
      end
    end
  end
  return topoary
end

SCCD.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = SCCD})
  obj:create(n)
  return obj
end

local n, m = io.read("*n", "*n")
local sccd = SCCD.new(2 * n)
local l, r = {}, {}
local linv, rinv = {}, {}
local state = {}
local bor = bit.bor
for i = 1, n do
  l[i], r[i] = io.read("*n", "*n")
  linv[i] = m - 1 - r[i]
  rinv[i] = m - 1 - l[i]
  state[i] = 0
end
local f1, f2, f3, f4 = true, true, true, true
for i = 1, n - 1 do
  for j = i + 1, n do
    f1 = l[i] <= r[j] and l[j] <= r[i]
    f2 = l[i] <= rinv[j] and linv[j] <= r[i]
    f3 = linv[i] <= r[j] and l[j] <= rinv[i]
    f4 = linv[i] <= rinv[j] and linv[j] <= rinv[i]

    if f1 and not f2 then sccd:addEdge(i, n + j) end
    if f2 and not f1 then sccd:addEdge(i, j) end
    if f3 and not f4 then sccd:addEdge(n + i, n + j) end
    if f4 and not f3 then sccd:addEdge(n + i, j) end
    if f1 and not f3 then sccd:addEdge(j, n + i) end
    if f3 and not f1 then sccd:addEdge(j, i) end
    if f2 and not f4 then sccd:addEdge(n + j, n + i) end
    if f4 and not f2 then sccd:addEdge(n + j, i) end

    if f1 and f2 then state[i] = bor(state[i], 1) end
    if f3 and f4 then state[i] = bor(state[i], 2) end
    if f1 and f3 then state[j] = bor(state[j], 1) end
    if f2 and f4 then state[j] = bor(state[j], 2) end
  end
end
for i = 1, n do
  if state[i] == 3 then
    print("NO") os.exit()
  end
  if state[i] == 1 then
    sccd:addEdge(i, n + i)
  elseif state[i] == 2 then
    sccd:addEdge(n + i, i)
  end
end
-- print(os.clock())

sccd:categorize()
sccd:toposort()
-- print(os.clock())
for i = 1, n do
  if sccd.root[i] == sccd.root[i + n] then
    print("NO") os.exit()
  end
end
print("YES")
0