結果
| 問題 |
No.2291 Union Find Estimate
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2023-05-06 00:15:00 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 358 ms / 2,000 ms |
| コード長 | 1,616 bytes |
| コンパイル時間 | 63 ms |
| コンパイル使用メモリ | 6,944 KB |
| 実行使用メモリ | 9,088 KB |
| 最終ジャッジ日時 | 2024-11-23 13:16:19 |
| 合計ジャッジ時間 | 1,712 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
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