結果

問題 No.2047 Path Factory
ユーザー ぷらぷら
提出日時 2022-08-19 22:35:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,186 bytes
コンパイル時間 2,319 ms
コンパイル使用メモリ 200,992 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-04-23 13:31:33
合計ジャッジ時間 4,057 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 57 ms
10,240 KB
testcase_08 AC 44 ms
8,704 KB
testcase_09 AC 52 ms
9,728 KB
testcase_10 AC 76 ms
12,288 KB
testcase_11 AC 44 ms
8,960 KB
testcase_12 AC 26 ms
6,528 KB
testcase_13 AC 27 ms
6,656 KB
testcase_14 AC 47 ms
8,960 KB
testcase_15 AC 56 ms
10,496 KB
testcase_16 AC 40 ms
9,216 KB
testcase_17 AC 40 ms
9,088 KB
testcase_18 AC 47 ms
10,880 KB
testcase_19 AC 56 ms
12,416 KB
testcase_20 AC 87 ms
16,512 KB
testcase_21 AC 48 ms
10,368 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 14 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 82 ms
26,624 KB
testcase_26 AC 110 ms
23,552 KB
testcase_27 AC 90 ms
14,336 KB
testcase_28 AC 63 ms
14,576 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int mod = 998244353;

long long modpow(long long a,long long b) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= mod;
        }
        (a *= a) %= mod;
        b /= 2;
    }
    return ans;
}

void dfs(int n,int p,vector<vector<int>>&ki,vector<vector<int>>&dp) {
    dp[n][0] = 1;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        dfs(i,n,ki,dp);
        vector<int>nxt(3);
        for(int j = 0; j < 3; j++) {
            nxt[j] += 1ll*dp[n][j]*(dp[i][1]+dp[i][2])%mod;
            if(nxt[j] >= mod) nxt[j] -= mod;
            if(j != 2) {
                nxt[j+1] += 1ll*dp[n][j]*(dp[i][0]+dp[i][1])%mod;
                if(nxt[j+1] >= mod) nxt[j+1] -= mod;
            }
        }
        dp[n] = nxt;
    }
}

int main() {
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int u,v;
        cin >> u >> v;
        u--;
        v--;
        ki[u].push_back(v);
        ki[v].push_back(u);
    }
    vector<vector<int>>dp(N,vector<int>(3));
    dfs(0,-1,ki,dp);
    cout << (dp[0][1]+dp[0][2])%mod << endl;
}
0