結果

問題 No.2047 Path Factory
ユーザー kyawakyawa
提出日時 2022-08-19 22:57:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 1,615 bytes
コンパイル時間 4,913 ms
コンパイル使用メモリ 260,772 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-04-23 13:32:15
合計ジャッジ時間 7,546 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 69 ms
7,552 KB
testcase_08 AC 51 ms
6,656 KB
testcase_09 AC 63 ms
7,552 KB
testcase_10 AC 98 ms
8,832 KB
testcase_11 AC 49 ms
6,784 KB
testcase_12 AC 30 ms
5,504 KB
testcase_13 AC 32 ms
5,376 KB
testcase_14 AC 54 ms
6,784 KB
testcase_15 AC 68 ms
7,552 KB
testcase_16 AC 53 ms
7,808 KB
testcase_17 AC 47 ms
7,552 KB
testcase_18 AC 63 ms
8,832 KB
testcase_19 AC 78 ms
10,240 KB
testcase_20 AC 128 ms
13,696 KB
testcase_21 AC 65 ms
8,448 KB
testcase_22 AC 12 ms
5,376 KB
testcase_23 AC 16 ms
5,376 KB
testcase_24 AC 14 ms
5,376 KB
testcase_25 AC 99 ms
26,624 KB
testcase_26 AC 156 ms
22,528 KB
testcase_27 AC 117 ms
11,336 KB
testcase_28 AC 63 ms
12,616 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include<atcoder/all>
using mint = atcoder::modint998244353;

int main(){
    int64_t N; cin >> N;
    vector<vector<int64_t>> edge(N);
    for(int64_t i = 0; i < N-1; i++){
        int64_t u, v; cin >> u >> v; u--; v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    vector<mint> dp1(N)/*親への遷移を切る*/, dp2(N)/*親への遷移を切らない*/;
    auto dfs = [&](auto &&self, int64_t vis, int64_t prv) -> void {
        for(auto nxt : edge[vis]){
            if(nxt == prv) continue;
            self(self, nxt, vis);
        }
        if(vis != 0 and edge[vis].size() == 1){
            dp1[vis] = 0;
            dp2[vis] = 1;
        }else{
            vector<int64_t> child;
            for(auto c : edge[vis]) if(c != prv) child.push_back(c);
            int64_t csiz = child.size();
            vector sub_count(3, vector(csiz, mint(0)));
            sub_count[0][0] = dp1[child[0]];
            sub_count[1][0] = dp2[child[0]];
            sub_count[2][0] = 0;
            for(int64_t i = 1; i < csiz; i++){
                sub_count[0][i] = sub_count[0][i-1] * dp1[child[i]];
                sub_count[1][i] = sub_count[1][i-1] * dp1[child[i]] + sub_count[0][i-1] * dp2[child[i]];
                sub_count[2][i] = sub_count[2][i-1] * dp1[child[i]] + sub_count[1][i-1] * dp2[child[i]];
            }
            dp1[vis] = sub_count[1][csiz-1] + sub_count[2][csiz-1];
            dp2[vis] = sub_count[0][csiz-1] + sub_count[1][csiz-1];
        }
    };
    dfs(dfs, 0, -1);
    cout << dp1[0].val() << '\n';
    return 0;
}
0