結果

問題 No.2291 Union Find Estimate
ユーザー 👑 obakyanobakyan
提出日時 2023-05-06 00:15:00
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 1,616 bytes
コンパイル時間 63 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-05-02 19:18:04
合計ジャッジ時間 1,581 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,248 KB
testcase_01 AC 6 ms
5,376 KB
testcase_02 AC 341 ms
7,168 KB
testcase_03 AC 46 ms
9,088 KB
testcase_04 AC 24 ms
6,784 KB
testcase_05 AC 26 ms
6,912 KB
testcase_06 AC 16 ms
6,016 KB
testcase_07 AC 11 ms
5,376 KB
testcase_08 AC 9 ms
5,888 KB
testcase_09 AC 9 ms
5,760 KB
testcase_10 AC 23 ms
7,040 KB
testcase_11 AC 41 ms
7,040 KB
testcase_12 AC 65 ms
7,168 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 33 ms
7,040 KB
testcase_15 AC 51 ms
8,064 KB
testcase_16 AC 31 ms
7,168 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 9 ms
5,376 KB
testcase_19 AC 24 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local UF = {}
UF.create = function(self, n)
  self.parent = {}
  for i = 1, n do
    self.parent[i] = i
  end
  self.gc = n - 10
  self.broken = false
end
UF.getroot = function(self, idx)
  local parent = self.parent
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end
UF.unite = function(self, a, b)
  local ra = self:getroot(a)
  local rb = self:getroot(b)
  local n = #self.parent
  if ra ~= rb then
    if ra < rb then
      self.parent[a], self.parent[ra] = rb, rb
      if n - 10 < ra then
        self.broken = true
      end
    else
      self.parent[b], self.parent[rb] = ra, ra
      if n - 10 < rb then
        self.broken = true
      end
    end
    self.gc = self.gc - 1
  end
end
UF.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = UF})
  obj:create(n)
  return obj
end
local p10 = {1}
for i = 2, 200010 do
  p10[i] = (p10[i - 1] * 10) % 998244353
end
local n, q = io.read("*n", "*n", "*l")
local uf = UF.new(n + 10)
local t = {}
for iq = 1, q do
  local s = io.read()
  for i = 1, 26 do
    t[i] = {}
  end
  for i = 1, n do
    local b = s:byte(i)
    if b <= 57 then
      b = b - 48 + 1
      uf:unite(i, n + b)
    elseif b ~= 63 then
      b = b - 96
      table.insert(t[b], i)
    end
  end
  for i = 1, 26 do
    if 1 < #t[i] then
      local r = uf:getroot(t[i][1])
      for j = 2, #t[i] do
        uf:unite(r, t[i][j])
      end
    end
  end
  if uf.broken then
    print(0)
  else
    print(p10[uf.gc + 1])
  end
end
0