結果

問題 No.2214 Products on Tree
ユーザー otoshigootoshigo
提出日時 2023-02-11 11:05:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 3,000 ms
コード長 1,281 bytes
コンパイル時間 1,937 ms
コンパイル使用メモリ 209,768 KB
実行使用メモリ 65,724 KB
最終ジャッジ日時 2024-07-08 01:40:13
合計ジャッジ時間 6,032 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 138 ms
24,192 KB
testcase_04 AC 134 ms
24,548 KB
testcase_05 AC 126 ms
23,552 KB
testcase_06 AC 80 ms
17,408 KB
testcase_07 AC 21 ms
7,424 KB
testcase_08 AC 14 ms
5,888 KB
testcase_09 AC 27 ms
8,704 KB
testcase_10 AC 74 ms
16,640 KB
testcase_11 AC 36 ms
10,368 KB
testcase_12 AC 45 ms
11,776 KB
testcase_13 AC 140 ms
25,176 KB
testcase_14 AC 136 ms
25,240 KB
testcase_15 AC 138 ms
25,360 KB
testcase_16 AC 149 ms
25,316 KB
testcase_17 AC 139 ms
25,216 KB
testcase_18 AC 67 ms
19,892 KB
testcase_19 AC 74 ms
21,208 KB
testcase_20 AC 64 ms
19,544 KB
testcase_21 AC 49 ms
16,096 KB
testcase_22 AC 98 ms
25,880 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 89 ms
49,024 KB
testcase_29 AC 20 ms
9,116 KB
testcase_30 AC 75 ms
32,116 KB
testcase_31 AC 112 ms
32,248 KB
testcase_32 AC 108 ms
29,436 KB
testcase_33 AC 116 ms
65,692 KB
testcase_34 AC 117 ms
65,724 KB
testcase_35 AC 141 ms
45,392 KB
testcase_36 AC 139 ms
45,440 KB
testcase_37 AC 93 ms
25,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
}
0