結果

問題 No.2047 Path Factory
ユーザー t98slidert98slider
提出日時 2022-08-24 10:50:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,039 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 172,220 KB
実行使用メモリ 28,312 KB
最終ジャッジ日時 2024-04-23 13:33:24
合計ジャッジ時間 3,476 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 36 ms
8,192 KB
testcase_08 AC 26 ms
7,168 KB
testcase_09 AC 31 ms
7,936 KB
testcase_10 AC 56 ms
9,728 KB
testcase_11 AC 26 ms
7,040 KB
testcase_12 AC 16 ms
6,944 KB
testcase_13 AC 16 ms
6,944 KB
testcase_14 AC 28 ms
7,424 KB
testcase_15 AC 34 ms
8,192 KB
testcase_16 AC 26 ms
8,320 KB
testcase_17 AC 25 ms
8,064 KB
testcase_18 AC 33 ms
9,728 KB
testcase_19 AC 44 ms
11,136 KB
testcase_20 AC 69 ms
14,848 KB
testcase_21 AC 32 ms
9,216 KB
testcase_22 AC 7 ms
6,940 KB
testcase_23 AC 9 ms
6,944 KB
testcase_24 AC 8 ms
6,940 KB
testcase_25 AC 48 ms
28,312 KB
testcase_26 AC 92 ms
23,936 KB
testcase_27 AC 68 ms
12,852 KB
testcase_28 AC 32 ms
14,964 KB
testcase_29 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, u, v;
    cin >> n;
    vector<vector<int>> g(n);
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        g[--u].emplace_back(--v);
        g[v].emplace_back(u);
    }
    vector<array<ll,3>> dp(n, {0, 0, 0});
    function<void(int,int)> dfs = [&](int v, int p){
        vector<array<ll,3>> a;
        for(auto &&u:g[v]){
            if(u == p)continue;
            dfs(u, v);
            a.push_back({dp[u][0], dp[u][1], dp[u][2]});
        }
        int c = a.size();
        dp[v][0] = 1;
        for(int i = 0; i < c; i++){
            array<ll,3> ndp{};
            for(int j = 0; j < 3; j++){
                if(j < 2)(ndp[j + 1] += dp[v][j] * (a[i][0] + a[i][1])) %= 998244353;
                (ndp[j] += dp[v][j] * (a[i][1] + a[i][2])) %= 998244353;
            }
            swap(dp[v], ndp);
        }
    };
    dfs(0, -1);
    cout << (dp[0][1] + dp[0][2]) % 998244353 << '\n';
}
0