結果

問題 No.1507 Road Blocked
ユーザー ぷらぷら
提出日時 2021-05-14 22:30:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 1,122 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 198,564 KB
実行使用メモリ 16,280 KB
最終ジャッジ日時 2024-04-10 01:02:36
合計ジャッジ時間 6,082 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 80 ms
16,280 KB
testcase_04 AC 85 ms
9,984 KB
testcase_05 AC 84 ms
10,112 KB
testcase_06 AC 86 ms
9,856 KB
testcase_07 AC 86 ms
9,856 KB
testcase_08 AC 85 ms
9,856 KB
testcase_09 AC 90 ms
9,984 KB
testcase_10 AC 89 ms
9,984 KB
testcase_11 AC 85 ms
9,880 KB
testcase_12 AC 91 ms
9,984 KB
testcase_13 AC 93 ms
9,856 KB
testcase_14 AC 87 ms
9,856 KB
testcase_15 AC 92 ms
10,080 KB
testcase_16 AC 90 ms
9,984 KB
testcase_17 AC 85 ms
9,856 KB
testcase_18 AC 90 ms
9,856 KB
testcase_19 AC 85 ms
9,996 KB
testcase_20 AC 84 ms
10,004 KB
testcase_21 AC 87 ms
9,856 KB
testcase_22 AC 91 ms
9,984 KB
testcase_23 AC 87 ms
10,112 KB
testcase_24 AC 87 ms
9,856 KB
testcase_25 AC 88 ms
9,956 KB
testcase_26 AC 86 ms
9,984 KB
testcase_27 AC 92 ms
9,984 KB
testcase_28 AC 91 ms
10,020 KB
testcase_29 AC 91 ms
9,856 KB
testcase_30 AC 92 ms
9,856 KB
testcase_31 AC 87 ms
9,856 KB
testcase_32 AC 88 ms
9,984 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