結果
| 問題 | 
                            No.1507 Road Blocked
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Manuel1024
                         | 
                    
| 提出日時 | 2021-05-16 22:31:19 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,276 bytes | 
| コンパイル時間 | 715 ms | 
| コンパイル使用メモリ | 74,252 KB | 
| 実行使用メモリ | 11,408 KB | 
| 最終ジャッジ日時 | 2024-10-05 09:42:19 | 
| 合計ジャッジ時間 | 4,749 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | WA * 30 | 
ソースコード
#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;
}
            
            
            
        
            
Manuel1024