#if defined(__GNUC__) && !defined(__clang__) #pragma GCC optimize("O3") #if defined(__x86_64__) || defined(__i386__) #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #endif #endif #include #include #define each(x, v) for (auto&& x : v) #define each2(x, y, v) for (auto&& [x, y] : v) #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(x) begin(x),end(x) #define sz(x) (int)(x).size() using namespace std; using namespace atcoder; using ll=long long; using ull=unsigned long long; using ld=long double; using i128=__int128; using vll=vector; using vvll=vector; using vi=vector; using vvi=vector; using pll=pair; using pi=pair; using mint=modint998244353; // using mint=modint1000000007; // using mint2=static_modint<2>; // using mint=LazyMontgomeryModInt<998244353>; using vm=vector; using vvm=vector; #line 2 "graph/graph-template.hpp" template struct edge { int src, to; T cost; edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {} edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template using Edges = vector>; template using WeightedGraph = vector>; using UnweightedGraph = vector>; // Input of (Unweighted) Graph UnweightedGraph graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { UnweightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; if (is_1origin) x--, y--; g[x].push_back(y); if (!is_directed) g[y].push_back(x); } return g; } // Input of Weighted Graph template WeightedGraph wgraph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { WeightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; cin >> c; if (is_1origin) x--, y--; g[x].emplace_back(x, y, c); if (!is_directed) g[y].emplace_back(y, x, c); } return g; } // Input of Edges template Edges esgraph([[maybe_unused]] int N, int M, int is_weighted = true, bool is_1origin = true) { Edges es; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; es.emplace_back(x, y, c); } return es; } // Input of Adjacency Matrix template vector> adjgraph(int N, int M, T INF, int is_weighted = true, bool is_directed = false, bool is_1origin = true) { vector> d(N, vector(N, INF)); for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; d[x][y] = c; if (!is_directed) d[y][x] = c; } return d; } /** * @brief グラフテンプレート * @docs docs/graph/graph-template.md */ int main() { ios::sync_with_stdio(false); cin.tie(nullptr); /** * O(N) * * 素直で木dpをすればいい * * r を根とする部分木を T[r] で表す * * f(r) : T[r] の答え * g(r) : T[r] - r の答え + T[r] かつ r の次数がちょうど 1 のような答え * * となれば * * f(r) = \sum_{子 u} g(u)\Prod_{子 x\neq u}f(x) + \sum_{子 u\neq v} g(u)*g(v)*\Prod_{子 x\neq u,v}f(x) * g(r) = \Prod_{子 u} f(u) + \sum_{子 u} g(u)\Prod_{子 x\neq u}f(x) */ int N; cin>>N; auto G=graph(N); vm f(N),g(N); auto dfs=[&](this auto&&self,int r,int p)->void { if(p!=-1&&sz(G[r])==1){ g[r]=1; }else{ mint suff=1; vi zero; each(u,G[r]){ if(u==p)continue; self(u,r); if(f[u]==0)zero.push_back(u); else suff*=f[u]; } if(sz(zero)==0){ mint sum=0,pref=1; each(u,G[r]){ if(u==p)continue; suff/=f[u]; g[r]+=g[u]*pref*suff; f[r]+=g[u]*sum*suff; sum*=f[u]; sum+=g[u]*pref; pref*=f[u]; } f[r]+=g[r]; g[r]+=pref; }else if(sz(zero)==1){ int a=zero[0]; g[r]=g[a]*suff; each(u,G[r]){ if(u==p||u==a)continue; f[r]+=g[a]*g[u]*suff/f[u]; } f[r]+=g[r]; }else if(sz(zero)==2){ int a=zero[0],b=zero[1]; f[r]=g[a]*g[b]*suff; } } }; dfs(0,-1); cout<