結果
問題 |
No.2214 Products on Tree
|
ユーザー |
![]() |
提出日時 | 2025-08-12 20:15:07 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 65 ms / 3,000 ms |
コード長 | 897 bytes |
コンパイル時間 | 1,797 ms |
コンパイル使用メモリ | 194,988 KB |
実行使用メモリ | 21,632 KB |
最終ジャッジ日時 | 2025-08-12 20:15:14 |
合計ジャッジ時間 | 6,162 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:36:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 36 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:39:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 39 | scanf("%d%d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long LL; constexpr int N = 2e5; constexpr int mod = 998244353; int n; class graph { private: int idx; public: int head[N + 5]; struct edge { int nxt, v; } e[(N << 1) + 5]; constexpr void add_dir(int u, int v) { e[++idx] = {head[u], v}; head[u] = idx; } constexpr void add_udir(int u, int v) { add_dir(u, v), add_dir(v, u); } } G; int f[N + 5], g[N + 5]; void dfs(int u, int fa = 0) { f[u] = g[u] = 1; for (int i = G.head[u]; i; i = G.e[i].nxt) { int v = G.e[i].v; if (v == fa) continue; dfs(v, u); int p = ((LL)f[u] * f[v] + (LL)f[u] * g[v] + (LL)g[u] * f[v]) % mod, q = ((LL)g[u] * f[v] + (LL)g[u] * g[v]) % mod; f[u] = p, g[u] = q; } } int main() { scanf("%d", &n); for (int _ = 1; _ < n; _++) { int u, v; scanf("%d%d", &u, &v); G.add_udir(u, v); } dfs(1); printf("%d\n", f[1]); return 0; }