結果

問題 No.2047 Path Factory
ユーザー kyawakyawa
提出日時 2022-08-19 22:56:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,682 bytes
コンパイル時間 3,976 ms
コンパイル使用メモリ 261,312 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2024-04-17 01:33:32
合計ジャッジ時間 6,096 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 52 ms
7,552 KB
testcase_08 AC 40 ms
6,784 KB
testcase_09 AC 48 ms
7,424 KB
testcase_10 AC 66 ms
8,832 KB
testcase_11 AC 41 ms
6,656 KB
testcase_12 AC 25 ms
5,376 KB
testcase_13 AC 25 ms
5,632 KB
testcase_14 AC 42 ms
7,040 KB
testcase_15 AC 52 ms
7,552 KB
testcase_16 AC 38 ms
7,680 KB
testcase_17 AC 37 ms
7,424 KB
testcase_18 AC 45 ms
9,088 KB
testcase_19 AC 55 ms
10,368 KB
testcase_20 AC 87 ms
13,440 KB
testcase_21 AC 45 ms
8,320 KB
testcase_22 AC 10 ms
5,376 KB
testcase_23 AC 13 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 84 ms
26,880 KB
testcase_26 AC 96 ms
22,400 KB
testcase_27 AC 78 ms
11,080 KB
testcase_28 AC 53 ms
12,744 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;
    if(N < 4){
        cout << -1 << '\n';
        return 0;
    }
    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