結果

問題 No.2214 Products on Tree
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2023-02-11 05:32:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 3,000 ms
コード長 1,404 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 175,568 KB
実行使用メモリ 20,740 KB
最終ジャッジ日時 2023-09-22 04:43:43
合計ジャッジ時間 11,580 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 276 ms
18,328 KB
testcase_04 AC 288 ms
18,412 KB
testcase_05 AC 284 ms
17,844 KB
testcase_06 AC 174 ms
13,260 KB
testcase_07 AC 42 ms
6,156 KB
testcase_08 AC 26 ms
5,164 KB
testcase_09 AC 59 ms
7,120 KB
testcase_10 AC 164 ms
12,932 KB
testcase_11 AC 79 ms
8,400 KB
testcase_12 AC 98 ms
9,116 KB
testcase_13 AC 297 ms
18,712 KB
testcase_14 AC 300 ms
18,756 KB
testcase_15 AC 300 ms
18,696 KB
testcase_16 AC 296 ms
18,860 KB
testcase_17 AC 300 ms
18,704 KB
testcase_18 AC 137 ms
14,852 KB
testcase_19 AC 151 ms
16,004 KB
testcase_20 AC 129 ms
14,672 KB
testcase_21 AC 97 ms
12,476 KB
testcase_22 AC 190 ms
19,356 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 129 ms
14,600 KB
testcase_29 AC 37 ms
6,732 KB
testcase_30 AC 145 ms
20,740 KB
testcase_31 AC 226 ms
20,716 KB
testcase_32 AC 196 ms
19,852 KB
testcase_33 AC 177 ms
18,728 KB
testcase_34 AC 179 ms
18,612 KB
testcase_35 AC 239 ms
18,680 KB
testcase_36 AC 239 ms
18,792 KB
testcase_37 AC 176 ms
19,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = std::vector<int> ;
using VVi = vector<Vi>;
using Vl = vector<ll>;
ll const m = 998244353;
ll mpow(ll a, ll n) {
    ll ret = 1;
    while (n) {
        if (n & 1) {
            ret = (ret * a) % m;
        }
        a = (a * a) % m;
        n >>= 1;
    }
    return ret;
}

int main () {
    int N;
    cin >> N;
    VVi tr(N);
    for (int i = 1; i < N; i ++) {
        int a, b;
        cin >> a >> b;
        tr[--a].push_back(--b);
        tr[b].push_back(a);
    }
    queue<int> que;
    Vi tpr;
    Vi par(N, -1);
    par[0] = -2;
    que.push(0);
    tpr.push_back(0);
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (auto v : tr[u]) {
            if (par[v] == -1) {
                par[v] = u;
                tpr.push_back(v);
                que.push(v);
            }
        }
    }
    reverse(tpr.begin(), tpr.end());
    Vl dp0(N, 0), dp1(N, 0);
    for (int i : tpr) {
        dp0[i] = dp1[i] = 1;
        for (auto v : tr[i]) {
            if (par[v] == i) {
                ll x = dp0[v] + dp1[v];
                x %= m;
                dp0[i] *= x;
                dp0[i] %= m;
                dp1[i] += (dp1[v] * mpow(x, m - 2)) % m;
                dp1[i] %= m;
            }
        }
        dp1[i] = (dp1[i] * dp0[i]) % m;
    }
    cout << dp1[0] << endl;
}
0