結果

問題 No.2047 Path Factory
ユーザー t98slider
提出日時 2022-08-24 10:50:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,039 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 175,256 KB
実行使用メモリ 28,160 KB
最終ジャッジ日時 2024-10-15 13:33:26
合計ジャッジ時間 3,414 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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