結果

問題 No.2047 Path Factory
ユーザー t98slidert98slider
提出日時 2022-08-24 10:50:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,039 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 172,704 KB
実行使用メモリ 26,428 KB
最終ジャッジ日時 2023-08-05 16:26:52
合計ジャッジ時間 3,433 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 29 ms
7,996 KB
testcase_08 AC 22 ms
7,004 KB
testcase_09 AC 27 ms
7,796 KB
testcase_10 AC 38 ms
9,648 KB
testcase_11 AC 23 ms
6,972 KB
testcase_12 AC 14 ms
5,436 KB
testcase_13 AC 15 ms
5,688 KB
testcase_14 AC 25 ms
7,272 KB
testcase_15 AC 27 ms
8,028 KB
testcase_16 AC 22 ms
8,004 KB
testcase_17 AC 21 ms
7,864 KB
testcase_18 AC 25 ms
9,412 KB
testcase_19 AC 29 ms
10,644 KB
testcase_20 AC 50 ms
14,404 KB
testcase_21 AC 26 ms
8,884 KB
testcase_22 AC 6 ms
4,752 KB
testcase_23 AC 8 ms
4,860 KB
testcase_24 AC 7 ms
4,592 KB
testcase_25 AC 46 ms
26,428 KB
testcase_26 AC 58 ms
22,624 KB
testcase_27 AC 43 ms
12,776 KB
testcase_28 AC 31 ms
15,604 KB
testcase_29 AC 2 ms
4,376 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