結果

問題 No.1507 Road Blocked
ユーザー Manuel1024Manuel1024
提出日時 2022-01-13 03:47:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,228 bytes
コンパイル時間 760 ms
コンパイル使用メモリ 74,440 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2024-11-15 22:27:24
合計ジャッジ時間 6,107 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

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