結果

問題 No.2214 Products on Tree
ユーザー i_am_noobi_am_noob
提出日時 2023-02-12 01:41:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 3,000 ms
コード長 1,107 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 201,208 KB
実行使用メモリ 40,880 KB
最終ジャッジ日時 2023-09-22 20:23:20
合計ジャッジ時間 9,254 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,500 KB
testcase_01 AC 5 ms
9,672 KB
testcase_02 AC 5 ms
9,432 KB
testcase_03 AC 146 ms
15,576 KB
testcase_04 AC 144 ms
15,784 KB
testcase_05 AC 126 ms
15,452 KB
testcase_06 AC 84 ms
13,572 KB
testcase_07 AC 22 ms
10,784 KB
testcase_08 AC 16 ms
10,236 KB
testcase_09 AC 28 ms
11,236 KB
testcase_10 AC 78 ms
13,464 KB
testcase_11 AC 37 ms
11,524 KB
testcase_12 AC 45 ms
12,004 KB
testcase_13 AC 158 ms
16,000 KB
testcase_14 AC 149 ms
16,072 KB
testcase_15 AC 150 ms
15,956 KB
testcase_16 AC 149 ms
16,016 KB
testcase_17 AC 151 ms
15,992 KB
testcase_18 AC 66 ms
14,712 KB
testcase_19 AC 75 ms
15,096 KB
testcase_20 AC 70 ms
14,632 KB
testcase_21 AC 47 ms
13,548 KB
testcase_22 AC 90 ms
16,572 KB
testcase_23 AC 5 ms
9,508 KB
testcase_24 AC 4 ms
9,676 KB
testcase_25 AC 4 ms
9,636 KB
testcase_26 AC 4 ms
9,512 KB
testcase_27 AC 4 ms
9,452 KB
testcase_28 AC 69 ms
32,344 KB
testcase_29 AC 19 ms
11,168 KB
testcase_30 AC 59 ms
17,916 KB
testcase_31 AC 98 ms
17,920 KB
testcase_32 AC 93 ms
17,260 KB
testcase_33 AC 94 ms
40,860 KB
testcase_34 AC 94 ms
40,880 KB
testcase_35 AC 129 ms
28,384 KB
testcase_36 AC 126 ms
28,376 KB
testcase_37 AC 79 ms
16,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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