結果

問題 No.1640 簡単な色塗り
ユーザー 👑 obakyanobakyan
提出日時 2021-08-07 22:30:52
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,188 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 5,212 KB
実行使用メモリ 29,588 KB
最終ジャッジ日時 2023-09-12 03:59:55
合計ジャッジ時間 24,154 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 148 ms
20,432 KB
testcase_05 AC 149 ms
20,412 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 604 ms
14,748 KB
testcase_11 AC 599 ms
13,108 KB
testcase_12 AC 345 ms
12,328 KB
testcase_13 AC 1,891 ms
23,380 KB
testcase_14 AC 1,275 ms
23,168 KB
testcase_15 AC 203 ms
8,808 KB
testcase_16 AC 288 ms
11,648 KB
testcase_17 AC 901 ms
21,044 KB
testcase_18 AC 27 ms
4,376 KB
testcase_19 AC 340 ms
12,436 KB
testcase_20 AC 812 ms
14,660 KB
testcase_21 AC 400 ms
12,940 KB
testcase_22 AC 15 ms
4,376 KB
testcase_23 AC 919 ms
15,328 KB
testcase_24 AC 80 ms
6,768 KB
testcase_25 AC 346 ms
12,700 KB
testcase_26 AC 775 ms
20,096 KB
testcase_27 AC 186 ms
8,592 KB
testcase_28 AC 1,170 ms
22,380 KB
testcase_29 AC 766 ms
20,040 KB
testcase_30 AC 19 ms
5,168 KB
testcase_31 AC 175 ms
24,400 KB
testcase_32 AC 149 ms
22,348 KB
testcase_33 AC 87 ms
14,420 KB
testcase_34 AC 139 ms
21,444 KB
testcase_35 AC 73 ms
13,328 KB
testcase_36 AC 19 ms
5,376 KB
testcase_37 AC 27 ms
7,400 KB
testcase_38 AC 144 ms
21,936 KB
testcase_39 AC 57 ms
11,508 KB
testcase_40 AC 51 ms
11,088 KB
testcase_41 AC 99 ms
19,660 KB
testcase_42 AC 57 ms
11,928 KB
testcase_43 AC 82 ms
13,292 KB
testcase_44 AC 70 ms
12,460 KB
testcase_45 AC 54 ms
11,332 KB
testcase_46 AC 19 ms
6,144 KB
testcase_47 AC 15 ms
4,948 KB
testcase_48 AC 177 ms
23,624 KB
testcase_49 AC 7 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 293 ms
25,600 KB
testcase_53 TLE -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

-- Hopcroft-Karp
ABmatch = {}
ABmatch.create = function(self, an, bn)
  self.dst = {}
  self.edge = {}
  for i = 1, an do
    self.dst[i] = false
    self.edge[i] = {}
  end
  self.src = {}
  self.invcnt = {}
  self.invs = {}
  self.invpos = {}
  self.padded = {}
  for i = 1, bn do
    self.src[i] = false
    self.invs[i] = {}
  end
  self.p = {}
  self.tasks = {}
  self.level = {}
end

ABmatch.addEdge = function(self, a, b)
  table.insert(self.edge[a], b)
  if not self.dst[a] and not self.src[b] then
    self.dst[a] = b
    self.src[b] = a
  end
end
ABmatch.makeAugPath = function(self)
  local tasknum = 0
  local tasks = self.tasks
  local level = self.level
  local edge = self.edge
  local src = self.src
  local an = #self.dst
  local bn = #src
  local invcnt, invs = self.invcnt, self.invs
  local p, psize = self.p, 0
  local padded = self.padded
  local lvinf = an + 1
  for i = 1, an do
    if self.dst[i] then
      level[i] = lvinf
    else
      tasknum = tasknum + 1
      tasks[tasknum] = i
      level[i] = 1
    end
  end
  for i = 1, bn do
    invcnt[i] = 0
    padded[i] = false
  end
  local done = 0
  local lvmax = lvinf
  while done < tasknum do
    done = done + 1
    local a = tasks[done]
    if lvmax < level[a] then break end
    for i = 1, #edge[a] do
      local b = edge[a][i]
      local sb = src[b]
      if sb then
        if level[sb] == lvinf then
          tasknum = tasknum + 1
          tasks[tasknum] = sb
          level[sb] = level[a] + 1
          invcnt[b] = invcnt[b] + 1
          invs[b][invcnt[b]] = a
        elseif level[sb] == level[a] + 1 then
          invcnt[b] = invcnt[b] + 1
          invs[b][invcnt[b]] = a
        end
      else
        lvmax = level[a]
        if padded[b] then
        else
          padded[b] = true
          psize = psize + 1
          p[psize] = b
        end
        invcnt[b] = invcnt[b] + 1
        invs[b][invcnt[b]] = a
      end
    end
  end
  self.psize = psize
  return lvmax
end

-- ABmatch.dfs = function(self, b, lv)
--   for i = 1, self.invcnt[b] do
--     local a = self.invs[b][i]
--     if self.level[a] == 1 or (self.level[a] == lv and self:dfs(self.dst[a], lv - 1)) then
--       self.dst[a], self.src[b] = b, a
--       self.level[a] = -1
--       return true
--     end
--   end
-- end

ABmatch.restore = function(self, bend, lvend)
  local invs, invcnt, invpos = self.invs, self.invcnt, self.invpos
  local dst, src, level = self.dst, self.src, self.level
  local bn = #src
  for i = 1, bn do
    invpos[i] = 0
  end
  local accept = false
  local tasks = self.tasks
  local taskpos = 1
  tasks[1] = bend
  while 0 < taskpos do
    local b = tasks[taskpos]
    if accept then
      local a = invs[b][invpos[b]]
      dst[a], src[b] = b, a
      level[a] = -1
      taskpos = taskpos - 1
    else
      if invpos[b] == invcnt[b] then
        level[src[b]] = -1
        taskpos = taskpos - 1
      else
        local ip = invpos[b] + 1
        invpos[b] = ip
        local a = invs[b][ip]
        if level[a] == 1 then
          accept = true
          dst[a], src[b] = b, a
          level[a] = -1
          taskpos = taskpos - 1
        elseif level[a] + taskpos == lvend + 1 then
          taskpos = taskpos + 1
          tasks[taskpos] = dst[a]
        end
      end
    end
  end
end

ABmatch.matching = function(self)
  local an = #self.dst
  local lv = self:makeAugPath()
  while lv <= an do
    for i = 1, self.psize do
      self:restore(self.p[i], lv)
      -- self:dfs(self.p[i], lv)
    end
    lv = self:makeAugPath()
  end
  self.executed = true
end

ABmatch.getMatchCount = function(self)
  if not self.executed then
    self:matching()
  end
  local cnt = 0
  for i = 1, #self.dst do
    if self.dst[i] then cnt = cnt + 1 end
  end
  return cnt
end

ABmatch.new = function(an, bn)
  local obj = {}
  setmetatable(obj, {__index = ABmatch})
  obj:create(an, bn)
  return obj
end

local n = io.read("*n")
local abm = ABmatch.new(n, n)
for i = 1, n do
  local a, b = io.read("*n", "*n")
  abm:addEdge(i, a)
  abm:addEdge(i, b)
end
if abm:getMatchCount() == n then
  print("Yes")
  for i = 1, n do
    print(abm.dst[i])
  end
else
  print("No")
end
-- print(os.clock())
0