QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#698384#7901. Basic Substring Structuretest_algthWA 4ms13520kbC++142.2kb2024-11-01 19:16:172024-11-01 19:16:20

Judging History

你现在查看的是最新测评结果

  • [2024-11-01 19:16:20]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:13520kb
  • [2024-11-01 19:16:17]
  • 提交

answer

#include <bits/stdc++.h>

const int MAXN = 1.01E5;
const int MAXB = 500;
const int P = 998244353;

inline int add(int a, int b) {
  return a -= (a += b) >= P ? P : 0;
}

inline void incr(int& a, int b) {
  a = add(a, b);
}

int dp[MAXN][MAXB + 1], sz[MAXN], ndp[MAXB + 1];
int N, K;
std::unordered_map <int, int> f[MAXN], g;
std::vector <int> adj[MAXN];

inline void merge(int u, int v) {
  for (int i = 0; i <= std::min(K + 1, sz[u] + sz[v]); ++i)
    ndp[i] = 0;
  
  for (int i = 0; i <= sz[u]; ++i) {
    for (int j = 0; j <= sz[v] && i + j <= K + 1; ++j) {
      incr(ndp[i + j], 1ll * dp[u][i] * dp[v][j] % P);
    }
    incr(ndp[i], 1ll * dp[u][i] * add(dp[v][K], dp[v][K + 1]) % P);
  }

  sz[u] += sz[v];
  for (int i = 0; i <= std::min(K + 1, sz[u]); ++i)
    dp[u][i] = ndp[i];
}

void solve(int u, int pa) {
  sz[u] = dp[u][1] = 1;
  for (int i = 2; i <= K; ++i)
    dp[u][i] = 0;
  for (int v : adj[u]) {
    if (v == pa) continue;
    solve(v, u);
    merge(u, v);
  }
}

inline void merge2(int u, int v) {
  g.clear();
  for (auto [a, b] : f[u]) {
    for (auto [c, d] : f[v]) {
      if (a + c <= K + 1) {
        incr(g[a + c], 1ll * b * d % P);
      }
    }
    
    if (f[v].count(K)) {
      incr(g[a], 1ll * b * f[v][K] % P);
    }

    if (f[v].count(K + 1)) {
      incr(g[a], 1ll * b * f[v][K + 1]);
    }
  }

  f[u] = g;
}

void solve2(int u, int pa) {
  f[u].clear();
  f[u][1] = 1;
  for (int v : adj[u]) {
    if (v == pa) continue;
    solve2(v, u);
    merge2(u, v);
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

  int testCase = 1;
  std::cin >> testCase;
  while (testCase--) {
    std::cin >> N >> K;
    for (int i = 1; i <= N; ++i) {
      adj[i].clear();
    }

    for (int i = 1, u, v; i < N; ++i) {
      std::cin >> u >> v;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }

    if (K < MAXB) { 
      solve(1, 1);
      std::cout << add(dp[1][K], dp[1][K + 1]) << '\n';
    }
    else {
      solve2(1, 1);
      std::cout << add(f[1][K], f[1][K + 1]) << '\n';
    }
  }

  return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 13520kb

input:

2
4
2 1 1 2
12
1 1 4 5 1 4 1 9 1 9 8 10

output:

0
0

result:

wrong answer 1st lines differ - expected: '15', found: '0'