結果

問題 No.2504 NOT Path Painting
ユーザー 👑 emthrmemthrm
提出日時 2023-08-12 17:50:38
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,613 bytes
コンパイル時間 1,033 ms
コンパイル使用メモリ 94,440 KB
実行使用メモリ 10,260 KB
最終ジャッジ日時 2023-10-13 18:15:08
合計ジャッジ時間 5,351 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 152 ms
4,348 KB
testcase_02 AC 146 ms
4,352 KB
testcase_03 AC 148 ms
4,348 KB
testcase_04 AC 148 ms
4,348 KB
testcase_05 AC 149 ms
4,352 KB
testcase_06 AC 161 ms
4,348 KB
testcase_07 AC 156 ms
4,348 KB
testcase_08 AC 149 ms
4,352 KB
testcase_09 AC 152 ms
4,348 KB
testcase_10 AC 147 ms
4,348 KB
testcase_11 AC 148 ms
4,348 KB
testcase_12 AC 141 ms
4,352 KB
testcase_13 AC 152 ms
4,348 KB
testcase_14 AC 158 ms
4,348 KB
testcase_15 AC 183 ms
5,556 KB
testcase_16 AC 187 ms
5,568 KB
testcase_17 AC 187 ms
5,608 KB
testcase_18 AC 174 ms
5,776 KB
testcase_19 AC 198 ms
9,792 KB
testcase_20 AC 180 ms
10,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <vector>

#include <atcoder/modint>
using mint = atcoder::modint998244353;

int ChoosePair(const int n) { return n * (n + 1) / 2; }

// <AC>
// 想定解
mint Solve(const std::vector<std::vector<int>>& tree) {
  const int n = tree.size(), denominator = ChoosePair(n);
  mint ans = 0;
  const auto CalcSubtree = [&tree, n, denominator, &ans](
      auto CalcSubtree, const int parent, const int vertex) -> int {
    int subtree = 1, p_v = denominator;
    for (const int child : tree[vertex]) {
      if (child != parent) {
        const int subtree_child = CalcSubtree(CalcSubtree, vertex, child);
        subtree += subtree_child;
        p_v -= ChoosePair(subtree_child);
        // 2 番目のシグマ
        ans -= mint(denominator)
               / (denominator - subtree_child * (n - subtree_child));
      }
    }
    if (parent != -1) p_v -= ChoosePair(n - subtree);
    ans += mint(denominator) / (denominator - p_v);  // 1 番目のシグマ
    return subtree;
  };
  assert(CalcSubtree(CalcSubtree, -1, 0) == n);
  return ans;
}

int main() {
  constexpr int kMaxT = 100000, kMaxN = 40000;

  int t;
  std::cin >> t;
  assert(1 <= t && t <= kMaxT);

  while (t--) {
    int n;
    std::cin >> n;
    assert(2 <= n && n <= kMaxN);
    std::vector<std::vector<int>> tree(n);
    for (int i = 0; i < n - 1; ++i) {
      int u, v;
      std::cin >> u >> v;
      assert(1 <= u && u <= n && 1 <= v && v <= n);
      --u; --v;
      tree[u].emplace_back(v);
      tree[v].emplace_back(u);
    }
    std::cout << Solve(tree).val() << '\n';
  }
  return 0;
}
0