結果
| 問題 |
No.2214 Products on Tree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-12 01:41:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 116 ms / 3,000 ms |
| コード長 | 1,107 bytes |
| コンパイル時間 | 1,735 ms |
| コンパイル使用メモリ | 197,272 KB |
| 最終ジャッジ日時 | 2025-02-10 14:37:47 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())
const int maxn=200005,mod=998244353;
int add(int x, int y){x+=y; if(x>=mod) x-=mod; return x;}
int mul(int x, int y){return ((ll)x)*y%mod;}
int n,dp[maxn][2];
vector<int> adj[maxn];
void dfs(int u, int par){
for(auto v: adj[u]) if(v!=par) dfs(v,u);
int tot=1;
vector<int> vec;
for(auto v: adj[u]) if(v!=par) tot=mul(tot,dp[v][0]),vec.pb(dp[v][0]);
dp[u][0]=add(tot,tot),dp[u][1]=tot;
int cur=0;
for(int i=sz(vec)-2; i>=0; --i) vec[i]=mul(vec[i],vec[i+1]);
vec.pb(1);
tot=1;
for(auto v: adj[u]) if(v!=par){
int de=mul(tot,mul(dp[v][1],vec[++cur]));
dp[u][0]=add(dp[u][0],de),dp[u][1]=add(dp[u][1],de);
tot=mul(tot,dp[v][0]);
}
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n;
for(int i=0; i<n-1; ++i){
int u,v; cin >> u >> v; u--,v--;
adj[u].pb(v),adj[v].pb(u);
}
dfs(0,-1);
cout << dp[0][1] << "\n";
}