結果

問題 No.2504 NOT Path Painting
ユーザー suisensuisen
提出日時 2023-02-21 09:46:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,928 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 99,956 KB
実行使用メモリ 814,208 KB
最終ジャッジ日時 2024-05-09 06:13:21
合計ジャッジ時間 3,585 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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
    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';
    
    return 0;
}
0