結果
| 問題 |
No.2504 NOT Path Painting
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-08-02 22:05:22 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,771 bytes |
| コンパイル時間 | 1,529 ms |
| コンパイル使用メモリ | 98,628 KB |
| 実行使用メモリ | 17,792 KB |
| 最終ジャッジ日時 | 2024-11-20 06:54:02 |
| 合計ジャッジ時間 | 30,385 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 TLE * 8 |
ソースコード
#include <cassert>
#include <iostream>
#include <vector>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
int ChoosePair(const int n) { return n * (n + 1) / 2; }
// <TLE>
// Õ(n^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> subtree(n); // 部分木の大きさ
const auto CalcSubtree = [&tree, &subtree](
auto CalcSubtree, const int parent, const int vertex) -> void {
subtree[vertex] = 1;
for (const int neighborhood : tree[vertex]) {
if (neighborhood != parent) {
CalcSubtree(CalcSubtree, vertex, neighborhood);
subtree[vertex] += subtree[neighborhood];
}
}
};
mint ans = 0;
// 最後の操作前にそのパスが白く塗られている期待値を求める
const auto dfs = [&tree, n, &denominator, F, &subtree, &ans](
auto dfs, const int root, const int child_of_root,
const int bad_close_to_root, const int bad_of_root,
const int parent, const int vertex, const int distance,
const int bad_choice) -> void {
int bad_close_to_vertex = 0;
for (const int child : tree[vertex]) {
if (child != parent) bad_close_to_vertex += ChoosePair(subtree[child]);
}
if (root < vertex) { // 一度だけカウントしたい
mint end_probability = 0;
if (distance == 1) {
assert(bad_choice == 0);
if (distance < n - 1) {
end_probability += bad_close_to_root + bad_close_to_vertex;
}
end_probability -= bad_close_to_root + ChoosePair(subtree[vertex]);
end_probability -= bad_of_root + bad_close_to_vertex;
} else {
if (distance < n - 1) {
end_probability +=
bad_choice + bad_close_to_root + bad_close_to_vertex;
}
end_probability -=
bad_choice + bad_close_to_root + ChoosePair(subtree[vertex]);
end_probability -= bad_choice + bad_of_root + bad_close_to_vertex;
end_probability +=
bad_choice + bad_of_root + ChoosePair(subtree[vertex]);
}
end_probability *= denominator;
ans += F((n - subtree[child_of_root]) * subtree[vertex] * denominator)
* end_probability;
}
for (const int child : tree[vertex]) {
if (child != parent) {
const int tmp = ChoosePair(subtree[child]);
dfs(dfs, root, child_of_root, bad_close_to_root, bad_of_root,
vertex, child, distance + 1,
bad_choice + bad_close_to_vertex - ChoosePair(subtree[child]));
}
}
};
for (int root = 0; root < n; ++root) {
CalcSubtree(CalcSubtree, -1, root);
int bad_choice = 0;
for (const int child : tree[root]) bad_choice += ChoosePair(subtree[child]);
for (const int child : tree[root]) {
dfs(dfs, root, child, bad_choice - ChoosePair(subtree[child]),
ChoosePair(n - subtree[child]), root, child, 1, 0);
}
// パス P = (root) のとき
ans += F(1 - bad_choice * denominator) * bad_choice * 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;
}
emthrm