結果
問題 | No.2504 NOT Path Painting |
ユーザー | suisen |
提出日時 | 2023-02-21 08:47:21 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 4,123 bytes |
コンパイル時間 | 1,844 ms |
コンパイル使用メモリ | 104,672 KB |
実行使用メモリ | 814,720 KB |
最終ジャッジ日時 | 2024-09-22 16:39:14 |
合計ジャッジ時間 | 4,282 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | MLE | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
ソースコード
#include <deque> #include <iostream> #include <tuple> #include <vector> #include <atcoder/modint> using mint = atcoder::modint998244353; int edge_num(int n) { return (n * (n + 1)) >> 1; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<std::vector<int>> g(n); for (int i = 0; i < n - 1; ++i) { int u, v; std::cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); } const int m = edge_num(n); const mint inv_m = mint(m).inv(); struct SubtreeSize { SubtreeSize(int n, const std::vector<std::vector<int>> &g) : _n(n), _par(_n, -1), _siz(_n, 1) { auto dfs = [&](auto dfs, int u, int p) -> int { _par[u] = p; for (int v : g[u]) if (v != p) { _siz[u] += dfs(dfs, v, u); } return _siz[u]; }; dfs(dfs, 0, -1); } int operator()(int u, int p) const { return _par[u] == p ? _siz[u] : _n - _siz[p]; } int t(int u, int ng1) const { return _n - (*this)(ng1, u); } int t(int u, int ng1, int ng2) const { return _n - (*this)(ng1, u) - (*this)(ng2, u); } private: int _n; std::vector<int> _par, _siz; } subtree_size { n, g }; std::vector<mint> ans_f(n, 0); for (int x = 0; x < n; ++x) { int u_x = edge_num(n); for (int y : g[x]) { u_x -= edge_num(subtree_size(y, x)); } ans_f[x] = m * mint(m - u_x).inv(); } std::vector<std::vector<mint>> ans_g(n, std::vector<mint>(n)); std::vector<std::vector<int>> par(n, std::vector<int>(n, -1)); // x, y, A, B std::deque<std::tuple<int, int, mint, mint>> dq; for (int x = 0; x < n; ++x) { ans_g[x][x] = ans_f[x]; int u_x = edge_num(n); for (int y : g[x]) { const int s_y = subtree_size(y, x); u_x -= edge_num(s_y); } for (int y : g[x]) { const int s_y = subtree_size(y, x); par[x][y] = x; const int u_y = u_x - s_y * (n - s_y); const mint A = u_y * ans_f[x]; const mint B = 0; dq.emplace_back(x, y, A, B); } } while (dq.size()) { auto [x, z, A, B] = dq.front(); dq.pop_front(); const int par_z = par[x][z]; const int s_z = subtree_size(z, par_z); int u_z = edge_num(s_z); for (int y : g[z]) if (y != par_z) { u_z -= edge_num(subtree_size(y, z)); } const int t_z = s_z; ans_g[x][z] = A + u_z * ans_f[z] + B; int prev_z2 = z, z2 = par_z; while (z2 != x) { const int next_z2 = par[x][z2]; const int t_z2 = subtree_size.t(z2, prev_z2, next_z2); ans_g[x][z] += t_z * t_z2 * ans_g[z][z2]; std::tie(prev_z2, z2) = std::make_tuple(z2, next_z2); } const int t_x = subtree_size.t(x, prev_z2); ans_g[x][z] = (1 + ans_g[x][z] * inv_m) * (1 - t_x * t_z * inv_m).inv(); for (int y : g[z]) if (y != par_z) { const int s_y = subtree_size(y, z); const int next_t_z = t_z - s_y; const int u_y = u_z - s_y * (s_z - s_y); const mint next_A = A + u_y * ans_f[z]; mint next_B = B + next_t_z * t_x * ans_g[x][z]; int prev_z2 = z, z2 = par_z; while (z2 != x) { const int next_z2 = par[x][z2]; const int t_z2 = subtree_size.t(z2, prev_z2, next_z2); next_B += next_t_z * t_z2 * ans_g[z][z2]; std::tie(prev_z2, z2) = std::make_tuple(z2, next_z2); } par[x][y] = z; dq.emplace_back(x, y, next_A, next_B); } } mint ans = 1; for (int x = 0; x < n; ++x) { for (int y = 0; y <= x; ++y) { ans += ans_g[x][y] * inv_m; } } std::cout << ans.val() << '\n'; return 0; }