結果

問題 No.1507 Road Blocked
ユーザー Manuel1024Manuel1024
提出日時 2022-01-13 04:21:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,190 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 74,564 KB
実行使用メモリ 13,904 KB
最終ジャッジ日時 2023-08-10 00:37:37
合計ジャッジ時間 6,318 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 70 ms
13,904 KB
testcase_04 AC 93 ms
9,232 KB
testcase_05 AC 84 ms
9,296 KB
testcase_06 AC 86 ms
9,336 KB
testcase_07 AC 87 ms
9,380 KB
testcase_08 AC 88 ms
9,340 KB
testcase_09 AC 94 ms
9,336 KB
testcase_10 AC 90 ms
9,292 KB
testcase_11 AC 92 ms
9,420 KB
testcase_12 AC 87 ms
9,240 KB
testcase_13 AC 87 ms
9,308 KB
testcase_14 AC 89 ms
9,420 KB
testcase_15 AC 90 ms
9,584 KB
testcase_16 AC 88 ms
9,372 KB
testcase_17 AC 92 ms
9,360 KB
testcase_18 AC 91 ms
9,440 KB
testcase_19 AC 94 ms
9,416 KB
testcase_20 AC 92 ms
9,608 KB
testcase_21 AC 90 ms
9,608 KB
testcase_22 AC 94 ms
9,428 KB
testcase_23 AC 95 ms
9,336 KB
testcase_24 AC 85 ms
9,296 KB
testcase_25 AC 89 ms
9,524 KB
testcase_26 AC 88 ms
9,288 KB
testcase_27 AC 90 ms
9,440 KB
testcase_28 AC 91 ms
9,316 KB
testcase_29 AC 90 ms
9,356 KB
testcase_30 AC 92 ms
9,308 KB
testcase_31 AC 89 ms
9,268 KB
testcase_32 AC 87 ms
9,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
constexpr ll MOD = 998244353;
graph G;
vector<ll> cnt;

ll modpow(ll a, ll x){
    ll ans = 1;
    while(x > 0){
        if(x & 1){
            ans *= a;
            ans %= MOD;
        }
        a *= a;
        a %= MOD;
        x >>= 1;
    }
    return ans;
}

int dfs(int c, int p){
    // cout << c << " ";
    if(cnt[c] != -1) return cnt[c];
    cnt[c] = 1;
    for(auto &nex: G[c]){
        if(nex == p) continue;
        cnt[c] += dfs(nex, c);
    }
    return cnt[c];
}

int main(){
    ll n;
    cin >> n;
    G = graph(n);
    cnt = vector<ll>(n, -1);
    //parent = vector<int>(n, -1);
    for(int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    // cerr << " ";
    dfs(0, -1);
    ll tot = 0;
    for(int i = 1; i < n; i++){
        tot += (n-cnt[i])*(n-cnt[i]-1);
        tot += cnt[i]*(cnt[i]-1);
        // cout << tot << " ";
        tot %= MOD;
    }

    cout << tot*modpow(n*(n-1)%MOD*(n-1)%MOD, MOD-2)%MOD << endl;
    return 0;
}
0