QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#262661#7684. Sweet SugarAshokaWA 158ms3408kbC++201.1kb2023-11-23 21:22:362023-11-23 21:22:36

Judging History

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

  • [2023-11-23 21:22:36]
  • 评测
  • 测评结果:WA
  • 用时:158ms
  • 内存:3408kb
  • [2023-11-23 21:22:36]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

constexpr i64 inf = 1E18;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  vector<vector<int>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u; --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  vector dp(n, array<i64, 2> ({0, -inf}));
  int ans = 0;
  auto dfs = [&](auto self, int u, int fa) -> void {
    dp[u][c[u] & 1] = c[u];
    for (auto v : adj[u]) {
      if (v == fa) continue;
      self(self, v, u);
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          dp[u][i ^ j] = max(dp[u][i ^ j], dp[v][i] + dp[u][j]);
        }
      }
    }
    if (dp[u][k & 1] >= k) {
      ans += 1;
      dp[u] = {0, -inf};
    }
    dp[u][0] = max(dp[u][0], 0LL);
    return ;
  };

  dfs(dfs, 0, -1);
  if (dp[0][k & 1] >= k) ++ans;
  cout << ans << "\n";
  return ;
} 

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    solve();
  } 
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3404kb

input:

4
7 5
1 2 1 2 2 1 2
1 2
2 3
3 4
3 5
5 6
5 7
2 2
1 0
1 2
1 1
1
1 2
1

output:

2
0
1
0

result:

ok 4 number(s): "2 0 1 0"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3376kb

input:

12
1 1
0
1 1
1
1 1
2
1 2
0
1 2
1
1 2
2
1 3
0
1 3
1
1 3
2
1 2000000
0
1 2000000
1
1 2000000
2

output:

0
1
0
0
0
1
0
0
0
0
0
0

result:

ok 12 numbers

Test #3:

score: -100
Wrong Answer
time: 158ms
memory: 3408kb

input:

200000
5 2
1 1 0 0 1
2 4
5 2
4 1
3 2
5 1
0 0 0 0 0
5 1
1 2
3 2
5 4
5 3
1 0 0 0 1
1 4
4 2
3 4
5 2
5 9
1 0 0 0 2
4 3
2 1
3 1
5 1
5 3
0 1 1 0 1
5 4
2 1
4 3
5 1
5 1
0 2 1 1 1
5 3
2 4
3 4
1 4
5 1
1 0 1 1 0
1 5
4 2
1 3
5 2
5 7
0 2 1 1 2
5 1
2 3
2 5
5 4
5 5
0 1 0 1 0
2 4
4 3
5 2
1 5
5 1
0 0 1 0 1
4 1
4 5
2...

output:

1
0
1
0
1
3
3
1
1
2
0
0
2
1
1
1
1
1
1
2
0
1
0
2
1
1
1
0
0
1
1
2
1
0
2
2
0
1
0
0
0
1
3
3
1
1
1
1
3
1
2
0
4
1
1
1
0
1
1
0
1
5
0
1
1
1
1
1
1
1
1
1
2
0
1
1
1
0
3
1
0
1
0
0
4
0
1
0
1
1
0
0
1
0
2
0
5
1
0
0
0
1
0
1
1
0
0
0
1
0
0
1
1
1
0
1
0
1
1
0
1
0
0
1
1
0
1
1
1
1
1
1
1
1
1
2
0
1
2
0
0
2
0
1
1
0
0
0
0
1
...

result:

wrong answer 3rd numbers differ - expected: '0', found: '1'