結果

問題 No.2047 Path Factory
ユーザー SSRSSSRS
提出日時 2022-08-19 21:46:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,534 bytes
コンパイル時間 1,904 ms
コンパイル使用メモリ 178,416 KB
実行使用メモリ 18,564 KB
最終ジャッジ日時 2024-04-17 00:27:24
合計ジャッジ時間 5,069 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 AC 2 ms
5,376 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 81 ms
11,644 KB
testcase_08 AC 63 ms
9,984 KB
testcase_09 AC 79 ms
11,264 KB
testcase_10 AC 116 ms
14,344 KB
testcase_11 AC 65 ms
9,856 KB
testcase_12 AC 36 ms
7,296 KB
testcase_13 AC 39 ms
7,552 KB
testcase_14 AC 70 ms
10,240 KB
testcase_15 AC 87 ms
11,776 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 98 ms
18,012 KB
testcase_26 AC 161 ms
18,012 KB
testcase_27 AC 141 ms
18,048 KB
testcase_28 AC 76 ms
18,564 KB
testcase_29 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(0);
  vector<int> bfs;
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    bfs.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<vector<long long>> dp(3, vector<long long>(N, 0));
  for (int v : bfs){
    int cnt = c[v].size();
    vector<vector<long long>> dp2(3, vector<long long>(cnt + 1, 0));
    dp2[0][0] = 1;
    for (int i = 0; i < cnt; i++){
      int w = c[v][i];
      dp2[1][i + 1] += dp2[0][i] * dp[0][w];
      dp2[2][i + 1] += dp2[1][i] * dp[0][w];
      dp2[0][i + 1] += dp2[0][i] * dp[1][w];
      dp2[1][i + 1] += dp2[1][i] * dp[1][w];
      dp2[2][i + 1] += dp2[2][i] * dp[1][w];
      dp2[1][i + 1] += dp2[0][i] * dp[1][w];
      dp2[2][i + 1] += dp2[1][i] * dp[1][w];
      dp2[0][i + 1] += dp2[0][i] * dp[2][w];
      dp2[1][i + 1] += dp2[1][i] * dp[2][w];
      dp2[2][i + 1] += dp2[2][i] * dp[2][w];
      dp2[0][i + 1] %= MOD;
      dp2[1][i + 1] %= MOD;
      dp2[1][i + 1] %= MOD;
    }
    for (int i = 0; i < 3; i++){
      dp[i][v] = dp2[i][cnt];
    }
  }
  cout << (dp[1][0] + dp[2][0]) % MOD << endl;
}
0