結果

問題 No.1507 Road Blocked
ユーザー ぷらぷら
提出日時 2021-05-14 22:30:56
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 1,122 bytes
コンパイル時間 1,840 ms
使用メモリ 16,032 KB
最終ジャッジ日時 2022-11-25 19:31:40
合計ジャッジ時間 5,538 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
5,872 KB
testcase_01 AC 3 ms
5,728 KB
testcase_02 AC 2 ms
5,776 KB
testcase_03 AC 62 ms
16,032 KB
testcase_04 AC 68 ms
9,848 KB
testcase_05 AC 68 ms
9,788 KB
testcase_06 AC 69 ms
9,884 KB
testcase_07 AC 65 ms
9,784 KB
testcase_08 AC 66 ms
9,752 KB
testcase_09 AC 68 ms
9,848 KB
testcase_10 AC 67 ms
9,776 KB
testcase_11 AC 67 ms
9,824 KB
testcase_12 AC 71 ms
9,776 KB
testcase_13 AC 71 ms
9,784 KB
testcase_14 AC 69 ms
9,916 KB
testcase_15 AC 65 ms
9,860 KB
testcase_16 AC 66 ms
9,788 KB
testcase_17 AC 68 ms
9,920 KB
testcase_18 AC 68 ms
9,804 KB
testcase_19 AC 67 ms
9,876 KB
testcase_20 AC 68 ms
9,836 KB
testcase_21 AC 67 ms
9,836 KB
testcase_22 AC 66 ms
9,832 KB
testcase_23 AC 65 ms
9,880 KB
testcase_24 AC 67 ms
9,780 KB
testcase_25 AC 69 ms
9,792 KB
testcase_26 AC 70 ms
9,796 KB
testcase_27 AC 67 ms
9,868 KB
testcase_28 AC 66 ms
9,824 KB
testcase_29 AC 66 ms
9,832 KB
testcase_30 AC 66 ms
9,820 KB
testcase_31 AC 66 ms
9,840 KB
testcase_32 AC 66 ms
9,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int mod = 998244353;

long long modpow(long long a,long long b) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= mod;
        }
        (a *= a) %= mod;
        b /= 2;
    }
    return ans;
}

int dp[100005];
vector<int>ki[100005];

int dfs(int n,int p) {
    for(int i = 0; i < ki[n].size(); i++) {
        if(ki[n][i] == p) {
            continue;
        }
        dp[n] += dfs(ki[n][i],n);
    }
    dp[n]++;
    return dp[n];
}

int main() {
    int N;
    cin >> N;
    vector<int>u(N-1),v(N-1);
    for(int i = 0; i < N-1; i++) {
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
        ki[u[i]].push_back(v[i]);
        ki[v[i]].push_back(u[i]);
    }
    dfs(0,-1);
    long long tmp1 = (long long)(N)*(N-1)/2%mod*(N-1)%mod,tmp2 = 0;
    for(int i = 0; i < N-1; i++) {
        int mi = min(dp[u[i]],dp[v[i]]);
        int mx = N-mi;
        tmp2 += (long long)(mi)*(mi-1)/2;
        tmp2 %= mod;
        tmp2 += (long long)(mx)*(mx-1)/2;
        tmp2 %= mod;
    }
    cout << tmp2*modpow(tmp1,mod-2)%mod << endl;
}
0