結果

問題 No.2504 NOT Path Painting
ユーザー suisensuisen
提出日時 2023-03-30 15:01:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,303 bytes
コンパイル時間 736 ms
コンパイル使用メモリ 79,392 KB
実行使用メモリ 814,144 KB
最終ジャッジ日時 2023-10-23 23:29:22
合計ジャッジ時間 3,930 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 <iostream>
#include <vector>

#include <atcoder/modint>

using mint = atcoder::modint998244353;

namespace atcoder {
    std::istream& operator>>(std::istream& in, mint &a) {
        long long e; in >> e; a = e;
        return in;
    }
    
    std::ostream& operator<<(std::ostream& out, const mint &a) {
        out << a.val();
        return out;
    }
} // namespace atcoder

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 mint m = n * (n + 1) / 2;

    mint ans = 0;
    auto dfs = [&](auto dfs, int u, int p) -> int {
        mint cnt = 0;

        int subu = 1;
        for (int v : g[u]) if (v != p) {
            int subv = dfs(dfs, v, u);
            ans -= mint(m - subv * (n - subv)).inv();
            cnt += subv * (subv + 1) / 2;
            subu += subv;
        }
        if (p != -1) {
            int subp = n - subu;
            cnt += subp * (subp + 1) / 2;
        }
        ans += cnt.inv();
        return subu;
    };
    dfs(dfs, 0, -1);
    
    std::cout << (m * ans).val() << std::endl;
}
0