結果

問題 No.2214 Products on Tree
ユーザー jianglyjiangly
提出日時 2023-02-10 21:53:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 108 ms / 3,000 ms
コード長 2,744 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 208,104 KB
実行使用メモリ 24,032 KB
最終ジャッジ日時 2023-09-21 23:03:34
合計ジャッジ時間 6,047 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 98 ms
15,120 KB
testcase_04 AC 96 ms
15,376 KB
testcase_05 AC 92 ms
14,520 KB
testcase_06 AC 59 ms
11,208 KB
testcase_07 AC 16 ms
5,488 KB
testcase_08 AC 11 ms
4,520 KB
testcase_09 AC 22 ms
6,152 KB
testcase_10 AC 54 ms
10,564 KB
testcase_11 AC 29 ms
6,972 KB
testcase_12 AC 34 ms
8,068 KB
testcase_13 AC 108 ms
15,608 KB
testcase_14 AC 103 ms
15,528 KB
testcase_15 AC 102 ms
15,608 KB
testcase_16 AC 103 ms
15,568 KB
testcase_17 AC 108 ms
15,804 KB
testcase_18 AC 47 ms
12,368 KB
testcase_19 AC 53 ms
13,328 KB
testcase_20 AC 45 ms
12,172 KB
testcase_21 AC 35 ms
10,376 KB
testcase_22 AC 70 ms
16,060 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 50 ms
18,224 KB
testcase_29 AC 14 ms
6,392 KB
testcase_30 AC 50 ms
16,480 KB
testcase_31 AC 72 ms
16,612 KB
testcase_32 AC 62 ms
16,484 KB
testcase_33 AC 68 ms
24,020 KB
testcase_34 AC 68 ms
24,032 KB
testcase_35 AC 92 ms
19,796 KB
testcase_36 AC 89 ms
19,956 KB
testcase_37 AC 64 ms
15,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

template<int P>
struct MInt {
    int x;
    MInt() : x{} {}
    MInt(i64 x) : x{norm(x % P)} {}
    
    int norm(int x) const {
        if (x < 0) {
            x += P;
        }
        if (x >= P) {
            x -= P;
        }
        return x;
    }
    int val() const {
        return x;
    }
    MInt operator-() const {
        MInt res;
        res.x = norm(P - x);
        return res;
    }
    MInt inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    MInt &operator*=(const MInt &rhs) {
        x = 1LL * x * rhs.x % P;
        return *this;
    }
    MInt &operator+=(const MInt &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    MInt &operator-=(const MInt &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    MInt &operator/=(const MInt &rhs) {
        return *this *= rhs.inv();
    }
    friend MInt operator*(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend MInt operator+(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend MInt operator-(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend MInt operator/(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

constexpr int P = 998244353;
using Z = MInt<P>;


int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<std::vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    std::vector<std::array<Z, 2>> dp(n);
    auto dfs = [&](auto self, int x, int p) -> void {
        dp[x][0] = dp[x][1] = 1;
        for (auto y : adj[x]) {
            if (y == p) {
                continue;
            }
            self(self, y, x);
            
            dp[x][1] = dp[x][1] * dp[y][1] + dp[x][0] * dp[y][1] + dp[x][1] * dp[y][0];
            dp[x][0] *= dp[y][0] + dp[y][1];
        }
    };
    dfs(dfs, 0, -1);
    
    std::cout << dp[0][1] << "\n";
    
    return 0;
}
0