結果
問題 | No.1507 Road Blocked |
ユーザー |
|
提出日時 | 2021-05-14 22:16:13 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 138 ms / 2,000 ms |
コード長 | 1,008 bytes |
コンパイル時間 | 1,410 ms |
コンパイル使用メモリ | 79,136 KB |
最終ジャッジ日時 | 2025-01-21 11:22:25 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <atcoder/modint>#include <iostream>#include <vector>using namespace std;using mint = atcoder::modint998244353;void solve() {int n;cin >> n;vector<vector<int>> graph(n);for (int i = n - 1; i--;) {int u, v;cin >> u >> v;--u, --v;graph[u].push_back(v);graph[v].push_back(u);}vector<int> sz(n, 1);{auto dfs = [&](auto&& f, int v, int p) -> int {auto& ret = sz[v];for (auto u : graph[v]) {if (u != p) ret += f(f, u, v);}return ret;};dfs(dfs, 0, -1);}mint ans = 0;mint coef = (mint(n) * (n - 1) * (n - 1)).inv();for (int v = 1; v < n; ++v) {ans += (mint(sz[v]) * (sz[v] - 1) +mint(n - sz[v]) * (n - sz[v] - 1)) *coef;}cout << ans.val() << "\n";}int main() {cin.tie(nullptr);ios::sync_with_stdio(false);solve();return 0;}