結果

問題 No.1507 Road Blocked
ユーザー hiro71687khiro71687k
提出日時 2023-03-31 00:35:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,228 bytes
コンパイル時間 5,387 ms
コンパイル使用メモリ 264,916 KB
実行使用メモリ 21,312 KB
最終ジャッジ日時 2023-10-22 07:32:06
合計ジャッジ時間 12,691 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 89 ms
21,312 KB
testcase_04 AC 115 ms
11,016 KB
testcase_05 AC 120 ms
11,016 KB
testcase_06 AC 120 ms
11,016 KB
testcase_07 AC 116 ms
11,016 KB
testcase_08 AC 116 ms
11,016 KB
testcase_09 AC 113 ms
11,016 KB
testcase_10 AC 115 ms
11,016 KB
testcase_11 AC 114 ms
11,016 KB
testcase_12 AC 114 ms
11,016 KB
testcase_13 AC 119 ms
11,016 KB
testcase_14 AC 115 ms
11,016 KB
testcase_15 AC 112 ms
11,016 KB
testcase_16 AC 113 ms
11,016 KB
testcase_17 AC 113 ms
11,016 KB
testcase_18 AC 112 ms
11,016 KB
testcase_19 AC 112 ms
11,016 KB
testcase_20 AC 105 ms
11,016 KB
testcase_21 AC 117 ms
11,016 KB
testcase_22 AC 110 ms
11,016 KB
testcase_23 AC 109 ms
11,016 KB
testcase_24 AC 113 ms
11,016 KB
testcase_25 AC 118 ms
11,016 KB
testcase_26 AC 109 ms
11,016 KB
testcase_27 AC 109 ms
11,016 KB
testcase_28 AC 114 ms
11,016 KB
testcase_29 AC 118 ms
11,016 KB
testcase_30 AC 114 ms
11,016 KB
testcase_31 AC 115 ms
11,016 KB
testcase_32 AC 118 ms
11,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll mod=998244353;
ll inf=999999999999999999;
ll bb,ee;
void dfs(ll now,vector<vector<ll>>&g,vector<ll>&dp,vector<ll>&ko){
    dp[now]=0;
    ko[now]=1;
    ll n=g.size();
    for (ll i = 0; i < g[now].size(); i++)
    {
        if (dp[g[now][i]]==-1)
        {
            dfs(g[now][i],g,dp,ko);
            ko[now]+=ko[g[now][i]];
            dp[now]+=dp[g[now][i]];
            dp[now]%=mod;
            ll y=(ko[g[now][i]]*(ko[g[now][i]]-1))/2;
            y%=mod;
            y+=((n-ko[g[now][i]])*(n-ko[g[now][i]]-1))/2;
            y%=mod;
            y*=bb;
            y%=mod;
            y*=ee;
            y%=mod;
            dp[now]+=y;
            dp[now]%=mod;
        }
    }
}
int main(){
    ll n;
    cin >> n;
    bb=inv_mod((n*(n-1))/2,mod);
    ee=inv_mod(n-1,mod);
    vector<vector<ll>>g(n);
    for (ll i = 0; i < n-1; i++)
    {
        ll u,v;
        cin >> u >> v;
        u--,v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<ll>dp(n,-1);
    vector<ll>ko(n,-1);
    dfs(0,g,dp,ko);
    cout << dp[0] << endl;
}
0