結果

問題 No.1507 Road Blocked
ユーザー trineutron
提出日時 2021-05-14 22:21:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 355 ms / 2,000 ms
コード長 869 bytes
コンパイル時間 6,348 ms
コンパイル使用メモリ 198,856 KB
最終ジャッジ日時 2025-01-21 11:27:34
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>

using namespace std;
using namespace atcoder;

using mint = modint998244353;
using graph = vector<vector<int>>;

int n;

mint dfs(const graph &to, vector<int> &sz, int v, int p)
{
    mint res = 0;
    for (auto &&c : to.at(v))
    {
        if (c == p)
        {
            continue;
        }
        res += dfs(to, sz, c, v);
        sz.at(v) += sz.at(c);
        res += mint(n) * (n - 1) / 2 - mint(sz.at(c)) * (n - sz.at(c));
    }
    sz.at(v)++;
    return res;
}

int main()
{
    cin >> n;
    graph to(n);
    for (int i = 0; i < n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        to.at(u).push_back(v);
        to.at(v).push_back(u);
    }
    vector<int> sz(n);
    cout << (dfs(to, sz, 0, -1) / (mint(n) * (n - 1) * (n - 1) / 2)).val() << endl;
    return 0;
}
0