Submission #32681890


Source Code Expand

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

typedef long long ll;
#define MOD 998244353
#define rep(i,a,n) for (ll i=a;i<(ll)n;i++)
#define per(i,a,n) for (ll i=n;i-->(ll)a;)
#define pb push_back

ll read(){ll r;scanf("%lld",&r);return r;} // read

vector<int> G[200010];

int main() {
  int n = read();
  rep(i,1,n){
    int u = read() - 1;
    int v = read() - 1;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  vector<int> fa(n,-1); // 父节点?
  vector<int> order={0}; // 树上bfs 顺序, 反序就是树上dp
  rep(i,0,n) {
    int u = order[i];
    for(auto v:G[u]){
      if(v == fa[u]) continue;
      fa[v] = u;
      order.push_back(v);
    }
  }

  vector<vector<ll>> dp(n,vector<ll>(4));
  per(i,0,n){
    int u = order[i];
    dp[u][1] = 1;
    ll all0 = 1;
    ll all3 = 1;
    ll pre1 = 0;
    for(auto v:G[u]){
      if(fa[v]!=u) continue;
      ll s12 = (dp[v][1]+dp[v][2])%MOD;
      // 0
      dp[u][0] = (dp[u][0] * dp[v][0] % MOD + all0 * s12 % MOD)%MOD;
      // 2
      dp[u][2] = (dp[u][2] * dp[v][3] % MOD + all3 * s12 % MOD)%MOD;

      dp[u][3] = (dp[u][3] * dp[v][3] % MOD + pre1 * s12 % MOD)%MOD;
      pre1     = (pre1     * dp[v][3] % MOD + all3 * s12 % MOD) %MOD;

      (all0 *= dp[v][0]) %= MOD;
      (all3 *= dp[v][3]) %= MOD;
    }
    // 1
    dp[u][1] = all0;
    (dp[u][3] *= 2) %= MOD;
  }
  printf("%lld\n",(dp[0][0] * 2 % MOD + dp[0][3])%MOD);
}

Submission Info

Submission Time
Task D - Deterministic Placing
User cromarmot
Language C++ (GCC 9.2.1)
Score 800
Code Size 1399 Byte
Status AC
Exec Time 196 ms
Memory 30428 KiB

Compile Error

./Main.cpp: In function ‘ll read()’:
./Main.cpp:10:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   10 | ll read(){ll r;scanf("%lld",&r);return r;} // read
      |                ~~~~~^~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 58
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_srnd_00.txt, 01_srnd_01.txt, 01_srnd_02.txt, 01_srnd_03.txt, 01_srnd_04.txt, 01_srnd_05.txt, 01_srnd_06.txt, 01_srnd_07.txt, 01_srnd_08.txt, 01_srnd_09.txt, 02_rnd_00.txt, 02_rnd_01.txt, 02_rnd_02.txt, 02_rnd_03.txt, 02_rnd_04.txt, 02_rnd_05.txt, 02_shand_00.txt, 02_shand_01.txt, 02_shand_02.txt, 02_shand_03.txt, 02_shand_04.txt, 03_path_00.txt, 03_path_01.txt, 03_rnd_00.txt, 04_path_00.txt, 04_path_01.txt, 04_star_00.txt, 05_matomo_00.txt, 05_matomo_01.txt, 05_matomo_02.txt, 05_matomo_03.txt, 05_matomo_04.txt, 05_matomo_05.txt, 05_matomo_06.txt, 05_matomo_07.txt, 05_matomo_08.txt, 05_matomo_09.txt, 05_matomo_10.txt, 05_matomo_11.txt, 05_star_00.txt, 05_star_01.txt, 05_star_02.txt, 05_star_03.txt, 06_matomo_00.txt, 06_matomo_01.txt, 06_matomo_02.txt, 06_matomo_03.txt, 06_matomo_04.txt, 06_matomo_05.txt, 06_matomo_06.txt, 06_matomo_07.txt, 06_matomo_08.txt, 06_matomo_09.txt, 06_matomo_10.txt, 06_matomo_11.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 12 ms 8328 KiB
00_sample_01.txt AC 7 ms 8272 KiB
00_sample_02.txt AC 6 ms 8364 KiB
01_srnd_00.txt AC 7 ms 8348 KiB
01_srnd_01.txt AC 7 ms 8448 KiB
01_srnd_02.txt AC 7 ms 8288 KiB
01_srnd_03.txt AC 5 ms 8288 KiB
01_srnd_04.txt AC 9 ms 8316 KiB
01_srnd_05.txt AC 7 ms 8292 KiB
01_srnd_06.txt AC 9 ms 8360 KiB
01_srnd_07.txt AC 12 ms 8340 KiB
01_srnd_08.txt AC 11 ms 8280 KiB
01_srnd_09.txt AC 7 ms 8272 KiB
02_rnd_00.txt AC 196 ms 29956 KiB
02_rnd_01.txt AC 189 ms 29824 KiB
02_rnd_02.txt AC 175 ms 29864 KiB
02_rnd_03.txt AC 182 ms 29848 KiB
02_rnd_04.txt AC 164 ms 29960 KiB
02_rnd_05.txt AC 176 ms 29892 KiB
02_shand_00.txt AC 7 ms 8348 KiB
02_shand_01.txt AC 7 ms 8276 KiB
02_shand_02.txt AC 8 ms 8452 KiB
02_shand_03.txt AC 9 ms 8272 KiB
02_shand_04.txt AC 7 ms 8292 KiB
03_path_00.txt AC 162 ms 29736 KiB
03_path_01.txt AC 81 ms 29636 KiB
03_rnd_00.txt AC 155 ms 29852 KiB
04_path_00.txt AC 168 ms 29552 KiB
04_path_01.txt AC 80 ms 29744 KiB
04_star_00.txt AC 73 ms 30280 KiB
05_matomo_00.txt AC 113 ms 26180 KiB
05_matomo_01.txt AC 125 ms 27916 KiB
05_matomo_02.txt AC 145 ms 29264 KiB
05_matomo_03.txt AC 118 ms 26348 KiB
05_matomo_04.txt AC 141 ms 29264 KiB
05_matomo_05.txt AC 135 ms 27092 KiB
05_matomo_06.txt AC 127 ms 28056 KiB
05_matomo_07.txt AC 138 ms 29452 KiB
05_matomo_08.txt AC 130 ms 28732 KiB
05_matomo_09.txt AC 132 ms 28180 KiB
05_matomo_10.txt AC 118 ms 26424 KiB
05_matomo_11.txt AC 141 ms 28404 KiB
05_star_00.txt AC 73 ms 30428 KiB
05_star_01.txt AC 165 ms 30144 KiB
05_star_02.txt AC 156 ms 30028 KiB
05_star_03.txt AC 150 ms 30148 KiB
06_matomo_00.txt AC 154 ms 29588 KiB
06_matomo_01.txt AC 146 ms 29644 KiB
06_matomo_02.txt AC 122 ms 25828 KiB
06_matomo_03.txt AC 97 ms 23032 KiB
06_matomo_04.txt AC 110 ms 24688 KiB
06_matomo_05.txt AC 138 ms 29544 KiB
06_matomo_06.txt AC 144 ms 29620 KiB
06_matomo_07.txt AC 148 ms 29348 KiB
06_matomo_08.txt AC 147 ms 29196 KiB
06_matomo_09.txt AC 132 ms 29296 KiB
06_matomo_10.txt AC 105 ms 24188 KiB
06_matomo_11.txt AC 156 ms 29464 KiB