結果

問題 No.2214 Products on Tree
ユーザー jianglyjiangly
提出日時 2023-02-10 21:53:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 99 ms / 3,000 ms
コード長 2,744 bytes
コンパイル時間 1,915 ms
コンパイル使用メモリ 202,612 KB
最終ジャッジ日時 2025-02-10 12:37:55
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0