結果

問題 No.2214 Products on Tree
ユーザー umimelumimel
提出日時 2023-02-14 01:27:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 3,000 ms
コード長 1,442 bytes
コンパイル時間 1,541 ms
コンパイル使用メモリ 175,884 KB
実行使用メモリ 40,748 KB
最終ジャッジ日時 2023-09-23 14:26:02
合計ジャッジ時間 9,749 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 191 ms
24,820 KB
testcase_04 AC 200 ms
25,288 KB
testcase_05 AC 188 ms
24,100 KB
testcase_06 AC 121 ms
17,820 KB
testcase_07 AC 27 ms
7,356 KB
testcase_08 AC 18 ms
6,024 KB
testcase_09 AC 37 ms
8,916 KB
testcase_10 AC 113 ms
17,092 KB
testcase_11 AC 51 ms
10,376 KB
testcase_12 AC 69 ms
11,956 KB
testcase_13 AC 216 ms
25,836 KB
testcase_14 AC 220 ms
25,948 KB
testcase_15 AC 217 ms
25,952 KB
testcase_16 AC 221 ms
26,068 KB
testcase_17 AC 212 ms
25,952 KB
testcase_18 AC 108 ms
19,820 KB
testcase_19 AC 120 ms
21,200 KB
testcase_20 AC 101 ms
19,296 KB
testcase_21 AC 76 ms
15,996 KB
testcase_22 AC 158 ms
25,592 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 88 ms
30,524 KB
testcase_29 AC 28 ms
8,528 KB
testcase_30 AC 100 ms
26,536 KB
testcase_31 AC 188 ms
26,780 KB
testcase_32 AC 176 ms
26,436 KB
testcase_33 AC 119 ms
40,532 KB
testcase_34 AC 120 ms
40,748 KB
testcase_35 AC 169 ms
33,468 KB
testcase_36 AC 174 ms
33,468 KB
testcase_37 AC 114 ms
26,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

ll pow_mod(ll x, ll n, ll mod){
    ll ret = 1;
    while(n > 0){
        if(n & 1) ret = (ret*x)%mod;
        x = x*x%mod;
        n >>=1;
    }
    return ret;
}

vector<vector<ll>> T;
vector<vector<ll>> dp;
void dfs(ll u, ll p){
    for(ll v : T[u]){
        if(v == p) continue;
        dfs(v, u);
    }

    ll sum = 1;
    for(ll v : T[u]){
        if(v == p) continue;
        sum *= (dp[v][0]+dp[v][1])%MOD2;
        sum %= MOD2;
    }

    dp[u][1] = sum;
    for(ll v : T[u]){
        if(v == p) continue;
        dp[u][0] *= (dp[v][0]+dp[v][1])%MOD2;
        dp[u][0] %= MOD2;
        ll s = (sum * pow_mod((dp[v][0]+dp[v][1])%MOD2, MOD2-2, MOD2))%MOD2;
        dp[u][1] += (s*dp[v][1])%MOD2;
        dp[u][1] %= MOD2;
    }

    return;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll n; cin >> n;
    T.resize(n);
    rep(i, n-1){
        ll a, b;
        cin >> a >> b;
        a--; b--;
        T[a].pb(b); T[b].pb(a);
    }

    dp.resize(n, vector<ll>(2, 1));
    dfs(0, -1);
    cout << dp[0][1] << endl;
}
0