結果

問題 No.1507 Road Blocked
ユーザー trineutrontrineutron
提出日時 2021-05-14 22:21:46
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 869 bytes
コンパイル時間 2,033 ms
使用メモリ 18,416 KB
最終ジャッジ日時 2022-11-25 19:10:01
合計ジャッジ時間 6,067 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
3,504 KB
testcase_01 AC 1 ms
3,536 KB
testcase_02 AC 1 ms
3,380 KB
testcase_03 AC 75 ms
18,416 KB
testcase_04 AC 82 ms
8,852 KB
testcase_05 AC 82 ms
8,852 KB
testcase_06 AC 102 ms
8,920 KB
testcase_07 AC 104 ms
8,848 KB
testcase_08 AC 98 ms
8,888 KB
testcase_09 AC 87 ms
8,964 KB
testcase_10 AC 87 ms
8,916 KB
testcase_11 AC 103 ms
8,924 KB
testcase_12 AC 110 ms
8,904 KB
testcase_13 AC 108 ms
8,856 KB
testcase_14 AC 87 ms
8,908 KB
testcase_15 AC 93 ms
8,908 KB
testcase_16 AC 81 ms
8,920 KB
testcase_17 AC 79 ms
8,908 KB
testcase_18 AC 80 ms
8,904 KB
testcase_19 AC 78 ms
8,992 KB
testcase_20 AC 81 ms
8,948 KB
testcase_21 AC 81 ms
8,948 KB
testcase_22 AC 83 ms
8,848 KB
testcase_23 AC 83 ms
8,892 KB
testcase_24 AC 79 ms
8,892 KB
testcase_25 AC 82 ms
8,924 KB
testcase_26 AC 81 ms
8,852 KB
testcase_27 AC 79 ms
8,892 KB
testcase_28 AC 82 ms
8,888 KB
testcase_29 AC 82 ms
8,908 KB
testcase_30 AC 82 ms
8,900 KB
testcase_31 AC 80 ms
8,920 KB
testcase_32 AC 82 ms
8,924 KB
権限があれば一括ダウンロードができます

ソースコード

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