結果

問題 No.1507 Road Blocked
ユーザー hiikunZhiikunZ
提出日時 2021-05-14 23:23:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 1,327 bytes
コンパイル時間 2,128 ms
コンパイル使用メモリ 199,480 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-04-10 03:46:02
合計ジャッジ時間 6,641 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 75 ms
14,208 KB
testcase_04 AC 102 ms
9,984 KB
testcase_05 AC 101 ms
9,984 KB
testcase_06 AC 100 ms
9,984 KB
testcase_07 AC 99 ms
10,172 KB
testcase_08 AC 100 ms
9,956 KB
testcase_09 AC 100 ms
10,112 KB
testcase_10 AC 100 ms
9,984 KB
testcase_11 AC 100 ms
9,984 KB
testcase_12 AC 99 ms
10,112 KB
testcase_13 AC 103 ms
9,984 KB
testcase_14 AC 100 ms
10,088 KB
testcase_15 AC 103 ms
9,984 KB
testcase_16 AC 100 ms
9,984 KB
testcase_17 AC 102 ms
9,984 KB
testcase_18 AC 104 ms
9,984 KB
testcase_19 AC 102 ms
10,112 KB
testcase_20 AC 100 ms
9,984 KB
testcase_21 AC 110 ms
9,984 KB
testcase_22 AC 104 ms
10,112 KB
testcase_23 AC 104 ms
9,984 KB
testcase_24 AC 104 ms
9,996 KB
testcase_25 AC 105 ms
10,068 KB
testcase_26 AC 108 ms
9,984 KB
testcase_27 AC 104 ms
9,984 KB
testcase_28 AC 104 ms
9,984 KB
testcase_29 AC 113 ms
10,112 KB
testcase_30 AC 104 ms
10,088 KB
testcase_31 AC 100 ms
9,984 KB
testcase_32 AC 100 ms
10,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 1000000000000000000;
const ll MOD = 998244353;
void randinit(){srand((unsigned)time(NULL));}
ll modinv(ll A,ll M){
    //A*r+B*n=gcd(A,B)
    A %= M;
    if(A == 0 || __gcd(A,M) != 1){
        //cout << "Error modinv(" << A << "," << M << ")" << endl;
        return -1;
    }
    ll B = M,U = 1,V = 0;
    while(B){
        ll T = A / B;
        A -= T * B;
        swap(A,B);
        U -= T * V;
        swap(U,V);
    }
    U %= M;
    if(U < 0) U += M;
    return U;
}
ll N;
vector<vector<ll>> G(100000);
vector<ll> S(100000,0);
ll s = 0;
ll dfs(ll n){
    if(S[n] == 1) return 0;
    S[n] = 1;
    ll ans = 1;
    for(ll i = 0;i < G[n].size();i++) ans += dfs(G[n][i]);
    if(n != 0) s += (ans * (ans - 1));
    if(n != 0)s += ((N - ans) * (N - ans - 1));
    s %= MOD;
    return ans;
}
int main(){
    cin >> N;
    for(ll i = 0;i < N - 1;i++){
        ll a,b;
        cin >> a >> b;
        G[a - 1].emplace_back(b - 1);
        G[b - 1].emplace_back(a - 1);
    }
    dfs(0);
    ll t = (N * (N - 1)) % MOD;
    t = (t * (N - 1)) % MOD;
    s = (s * modinv(2,MOD)) % MOD;
    t = (t * modinv(2,MOD)) % MOD;
    cout << (s * modinv(t,MOD)) % MOD << endl;
}
0