結果

問題 No.1507 Road Blocked
ユーザー Manuel1024Manuel1024
提出日時 2022-01-13 03:47:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,228 bytes
コンパイル時間 697 ms
コンパイル使用メモリ 73,844 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-04-27 18:06:24
合計ジャッジ時間 4,932 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 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;
using P = pair<int, int>;
using graph = vector<vector<int>>;
constexpr ll MOD = 998244353;
graph G;
vector<int> cnt, parent;

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;
}

ll mygcd(ll a, ll b){
    while(b != 0){
        ll r = a%b;
        a = b;
        b = r;
    }
    return a;
}

int dfs(int c, int p){
    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<int>(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);
    }
    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);
        tot %= MOD;
    }

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