結果

問題 No.2504 NOT Path Painting
ユーザー 👑 emthrmemthrm
提出日時 2023-08-03 12:40:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 2,159 bytes
コンパイル時間 1,307 ms
コンパイル使用メモリ 97,780 KB
実行使用メモリ 8,584 KB
最終ジャッジ日時 2023-10-13 18:15:05
合計ジャッジ時間 6,785 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 175 ms
4,348 KB
testcase_02 AC 156 ms
4,348 KB
testcase_03 AC 159 ms
4,352 KB
testcase_04 AC 158 ms
4,348 KB
testcase_05 AC 159 ms
4,352 KB
testcase_06 AC 163 ms
4,348 KB
testcase_07 AC 159 ms
4,348 KB
testcase_08 AC 161 ms
4,356 KB
testcase_09 AC 163 ms
4,352 KB
testcase_10 AC 159 ms
4,352 KB
testcase_11 AC 155 ms
4,352 KB
testcase_12 AC 159 ms
4,356 KB
testcase_13 AC 158 ms
4,348 KB
testcase_14 AC 164 ms
4,352 KB
testcase_15 AC 202 ms
5,784 KB
testcase_16 AC 202 ms
5,980 KB
testcase_17 AC 199 ms
5,888 KB
testcase_18 AC 177 ms
6,064 KB
testcase_19 AC 207 ms
8,120 KB
testcase_20 AC 190 ms
8,584 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; }

mint Solve(const std::vector<std::vector<int>>& tree) {
  const int n = tree.size();
  const mint denominator = mint(ChoosePair(n)).inv();

  const auto F = [](const mint& probability) -> mint {
    return probability * (2 - probability) * (1 - probability).pow(2).inv();
  };

  std::vector<int> parent(n, -1);
  std::vector<int> subtree(n, 1);  // 根を頂点 1 とする部分木の大きさ
  const auto CalcSubtree = [&tree, &parent, &subtree](
      auto CalcSubtree, const int vertex) -> void {
    for (const int neighborhood : tree[vertex]) {
      if (neighborhood != parent[vertex]) {
        parent[neighborhood] = vertex;
        CalcSubtree(CalcSubtree, neighborhood);
        subtree[vertex] += subtree[neighborhood];
      }
    }
  };
  CalcSubtree(CalcSubtree, 0);

  mint ans = 0;

  // 長さ 0 のパス
  for (int i = 0; i < n; ++i) {
    int num_of_path = 0;
    for (const int neighborhood : tree[i]) {
      num_of_path += ChoosePair(neighborhood == parent[i] ?
                                n - subtree[i] : subtree[neighborhood]);
    }
    ans += F(1 - num_of_path * denominator) * num_of_path * denominator;
  }

  // 長さ 1 のパス
  for (int i = 0; i < n; ++i) {
    for (const int j : tree[i]) {
      if (j == parent[i]) continue;
      ans += F((n - subtree[j]) * subtree[j] * denominator)
             * (-ChoosePair(n - subtree[j]) - ChoosePair(subtree[j]))
             * denominator;
    }
  }

  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