結果

問題 No.1507 Road Blocked
ユーザー Manuel1024Manuel1024
提出日時 2021-05-16 22:31:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,276 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 75,884 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-15 13:15:24
合計ジャッジ時間 6,240 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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 dfs1(int cur, int parent){
    int ans = 1;
    for(auto &p: G[cur]){
        if(p.first == parent) continue;
        else{
            p.second = dfs1(p.first, cur);
            ans += p.second;
        }
    }
    return ans;
}

int main(){
    int n;
    cin >> n;
    G = graph(n);
    for(int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(make_pair(v, -1));
        G[v].push_back(make_pair(u, -1));
    }
    dfs1(0, -1);

    ll cnt1 = 0;
    for(auto &p: G){
        for(auto &pp: p){
            if(pp.second == -1) continue;
            cnt1 += (ll)pp.second*(pp.second-1);
            cnt1 += (ll)(n-pp.second)*(n-pp.second-1);
        }
    }
    //cerr << cnt1 << " " << n*(n-1)*(n-1) << endl;

    ll ans = cnt1%MOD;
    ans *= modpow((n*(n-1)*(n-1))%MOD, MOD-2);
    cout << ans%MOD << endl;
    return 0;
}
0