結果
| 問題 | 
                            No.1507 Road Blocked
                             | 
                    
| コンテスト | |
| ユーザー | 
                            👑  | 
                    
| 提出日時 | 2021-05-16 20:05:35 | 
| 言語 | Lua  (LuaJit 2.1.1734355927)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 238 ms / 2,000 ms | 
| コード長 | 1,945 bytes | 
| コンパイル時間 | 83 ms | 
| コンパイル使用メモリ | 6,948 KB | 
| 実行使用メモリ | 21,760 KB | 
| 最終ジャッジ日時 | 2024-10-05 05:55:31 | 
| 合計ジャッジ時間 | 8,838 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 | 
ソースコード
local mod = 998244353
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31596, y % 31596
  local x1, y1 = mfl(x / 31596), mfl(y / 31596)
  return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end
local function bsub(x, y)
  return (x + mod - y) % mod
end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end
local function modinv(src)
  return modpow(src, mod - 2)
end
local n = io.read("*n")
local edge = {}
local parent = {}
local childask = {}
local childcnt = {}
for i = 1, n do
  edge[i] = {}
  parent[i] = 0
  childask[i] = 0
  childcnt[i] = 0
end
local ea, eb, ec = {}, {}, {}
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  ea[i] = a
  eb[i] = b
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if parent[src] ~= dst then
      parent[dst] = src
      table.insert(tasks, dst)
      childask[src] = childask[src] + 1
    end
  end
end
tasks = {}
for i = 2, n do
  if childask[i] == 0 then
    table.insert(tasks, i)
  end
end
local ret = 0
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local child = edge[src][j]
    if child ~= parent[src] then
      local a = childcnt[child] + 1
      local b = n - a
      local numer = badd(bmul(a, a - 1), bmul(b, b - 1))
      local denom = badd(numer, bmul(2 * a, b))
      ret = badd(ret, bmul(numer, modinv(denom)))
    end
  end
  local p = parent[src]
  if 0 < p then
    childask[p] = childask[p] - 1
    childcnt[p] = childcnt[p] + 1 + childcnt[src]
    if childask[p] == 0 then
      table.insert(tasks, p)
    end
  end
end
ret = bmul(ret, modinv(n - 1))
print(ret)