結果

問題 No.2504 NOT Path Painting
ユーザー suisensuisen
提出日時 2023-07-22 17:45:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,319 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 100,932 KB
実行使用メモリ 8,860 KB
最終ジャッジ日時 2023-10-24 00:14:40
合計ジャッジ時間 16,487 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 763 ms
4,372 KB
testcase_02 AC 859 ms
4,372 KB
testcase_03 AC 867 ms
4,372 KB
testcase_04 AC 867 ms
4,372 KB
testcase_05 AC 872 ms
4,372 KB
testcase_06 AC 868 ms
4,372 KB
testcase_07 AC 871 ms
4,372 KB
testcase_08 AC 869 ms
4,372 KB
testcase_09 AC 865 ms
4,372 KB
testcase_10 AC 967 ms
4,372 KB
testcase_11 AC 859 ms
4,372 KB
testcase_12 AC 964 ms
4,372 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// TLE 区間DP 苦行

#include <deque>
#include <iostream>
#include <tuple>
#include <vector>

#include <atcoder/modint>

using mint = atcoder::modint998244353;

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;
};

int edge_num(int n) {
    return (n * (n + 1)) >> 1;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    auto solve = [&]{
        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();

        SubtreeSize 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
        std::deque<std::tuple<int, int>> dq;
        for (int x = 0; x < n; ++x) {
            ans_g[x][x] = ans_f[x];
            for (int y : g[x]) {
                par[x][y] = x;
                dq.emplace_back(x, y);
            }
        }

        while (dq.size()) {
            auto [x, z] = dq.front();
            dq.pop_front();

            std::vector<int> Pxz;
            for (int a = z; a != -1; a = par[x][a]) {
                Pxz.push_back(a);
            }
            assert(Pxz.front() == z and Pxz.back() == x and Pxz.size() >= 2);
            const int l = Pxz.size();
            auto get_t_z = [&](int idx) {
                return
                    idx == 0        ? subtree_size.t(Pxz[idx], Pxz[idx + 1])
                :   idx == l - 1    ? subtree_size.t(Pxz[idx], Pxz[idx - 1])
                :   subtree_size.t(Pxz[idx], Pxz[idx - 1], Pxz[idx + 1]);
            };

            for (int i = 0; i < l; ++i) {
                const int z2 = Pxz[i];

                std::vector<int> ng;
                if (i > 0) ng.push_back(Pxz[i - 1]);
                if (i + 1 < l) ng.push_back(Pxz[i + 1]);

                int u_z2 = 1;
                {
                    int sum = 0;
                    for (int nz : g[z2]) if (nz != ng.front() and nz != ng.back()) {
                        int s_nz = subtree_size(nz, z2);
                        u_z2 += sum * s_nz;
                        sum += s_nz;
                    }
                    u_z2 += sum;
                }
                ans_g[x][z] += u_z2 * ans_f[z2];
                const int t2 = get_t_z(i);

                for (int j = 0; j < i; ++j) {
                    if (i == l - 1 and j == 0) continue;
                    const int z1 = Pxz[j];
                    const int t1 = get_t_z(j);
                    ans_g[x][z] += t1 * t2 * ans_g[z2][z1];
                }
            }
            ans_g[x][z] = (1 + ans_g[x][z] * inv_m) * (1 - get_t_z(l - 1) * get_t_z(0) * inv_m).inv();

            for (int y : g[z]) if (y != par[x][z]) {
                par[x][y] = z;
                dq.emplace_back(x, y);
            }
        }

        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';
    };
    
    int t;
    std::cin >> t;
    while (t --> 0) {
        solve();
    }
    
    return 0;
}
0