結果
問題 | 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 = 998244353local mfl = math.floorlocal function bmul(x, y)local x0, y0 = x % 31596, y % 31596local x1, y1 = mfl(x / 31596), mfl(y / 31596)return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % modendlocal function badd(x, y)return (x + y) % modendlocal function bsub(x, y)return (x + mod - y) % modendlocal function modpow(src, pow)local res = 1while 0 < pow doif pow % 2 == 1 thenres = bmul(res, src)pow = pow - 1endsrc = bmul(src, src)pow = mfl(pow / 2)endreturn resendlocal function modinv(src)return modpow(src, mod - 2)endlocal n = io.read("*n")local edge = {}local parent = {}local childask = {}local childcnt = {}for i = 1, n doedge[i] = {}parent[i] = 0childask[i] = 0childcnt[i] = 0endlocal ea, eb, ec = {}, {}, {}for i = 1, n - 1 dolocal a, b = io.read("*n", "*n")ea[i] = aeb[i] = btable.insert(edge[a], b)table.insert(edge[b], a)endlocal tasks = {1}for i = 1, n dolocal src = tasks[i]for j = 1, #edge[src] dolocal dst = edge[src][j]if parent[src] ~= dst thenparent[dst] = srctable.insert(tasks, dst)childask[src] = childask[src] + 1endendendtasks = {}for i = 2, n doif childask[i] == 0 thentable.insert(tasks, i)endendlocal ret = 0for i = 1, n dolocal src = tasks[i]for j = 1, #edge[src] dolocal child = edge[src][j]if child ~= parent[src] thenlocal a = childcnt[child] + 1local b = n - alocal 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)))endendlocal p = parent[src]if 0 < p thenchildask[p] = childask[p] - 1childcnt[p] = childcnt[p] + 1 + childcnt[src]if childask[p] == 0 thentable.insert(tasks, p)endendendret = bmul(ret, modinv(n - 1))print(ret)