結果

問題 No.2214 Products on Tree
ユーザー otoshigootoshigo
提出日時 2023-02-11 11:05:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 3,000 ms
コード長 1,281 bytes
コンパイル時間 2,536 ms
コンパイル使用メモリ 207,040 KB
実行使用メモリ 62,552 KB
最終ジャッジ日時 2023-09-22 09:32:33
合計ジャッジ時間 9,136 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 203 ms
23,940 KB
testcase_04 AC 191 ms
24,508 KB
testcase_05 AC 175 ms
23,204 KB
testcase_06 AC 106 ms
17,272 KB
testcase_07 AC 25 ms
7,400 KB
testcase_08 AC 17 ms
5,628 KB
testcase_09 AC 37 ms
8,576 KB
testcase_10 AC 95 ms
16,380 KB
testcase_11 AC 49 ms
10,136 KB
testcase_12 AC 63 ms
11,428 KB
testcase_13 AC 170 ms
25,064 KB
testcase_14 AC 174 ms
25,008 KB
testcase_15 AC 188 ms
24,952 KB
testcase_16 AC 201 ms
24,960 KB
testcase_17 AC 205 ms
25,108 KB
testcase_18 AC 86 ms
19,876 KB
testcase_19 AC 87 ms
21,100 KB
testcase_20 AC 84 ms
19,340 KB
testcase_21 AC 69 ms
16,188 KB
testcase_22 AC 119 ms
26,136 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 94 ms
46,444 KB
testcase_29 AC 27 ms
8,972 KB
testcase_30 AC 83 ms
32,000 KB
testcase_31 AC 144 ms
31,936 KB
testcase_32 AC 128 ms
29,420 KB
testcase_33 AC 133 ms
62,552 KB
testcase_34 AC 132 ms
62,448 KB
testcase_35 AC 213 ms
43,784 KB
testcase_36 AC 209 ms
43,820 KB
testcase_37 AC 109 ms
25,016 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