結果
問題 |
No.2214 Products on Tree
|
ユーザー |
|
提出日時 | 2023-02-11 11:05:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 156 ms / 3,000 ms |
コード長 | 1,281 bytes |
コンパイル時間 | 1,906 ms |
コンパイル使用メモリ | 201,996 KB |
最終ジャッジ日時 | 2025-02-10 14:13:32 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} const ll MOD=998244353; void dfs(int x,int p,vector<vector<int>>&g,vector<vector<ll>>&dp){ vector<ll>A,B; for(auto i:g[x]){ if(i==p)continue; dfs(i,x,g,dp); A.push_back(dp[i][1]); B.push_back(dp[i][0]); } int l=A.size(); vector<ll>L(l+1),R(l+1); L[0]=R[l]=1; rep(i,l)L[i+1]=L[i]*(A[i]+B[i])%MOD; rrep(i,l)R[i]=R[i+1]*(A[i]+B[i])%MOD; dp[x][0]=dp[x][1]=L[l]; rep(i,l){ dp[x][1]+=A[i]*(L[i]*R[i+1]%MOD)%MOD; dp[x][1]%=MOD; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin>>n; vector<vector<int>>g(n); rep(i,n-1){ int a,b; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vector<vector<ll>>dp(n,vector<ll>(2)); dfs(0,-1,g,dp); ll ans=dp[0][1]; cout<<ans<<"\n"; }