QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#487755 | #7684. Sweet Sugar | yaoy | WA | 148ms | 3880kb | C++20 | 2.1kb | 2024-07-23 09:36:16 | 2024-07-23 09:36:16 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, k;
std::cin >> n >> k;
std::vector<int> c(n);
for (int i = 0; i < n; i++) {
std::cin >> c[i];
}
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--, v--;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
int ans = 0;
std::vector<std::array<int, 2>> dp(n);
auto dfs = [&](auto self, int u, int f) -> void {
for (auto v : adj[u]) {
if (v == f) {
continue;
}
self(self, v, u);
if (dp[v][k % 2] >= k) {
ans++;
continue;
}
auto ndp = dp[u];
ndp[0] = std::max(ndp[0], dp[u][0] + dp[v][0]);
if (dp[v][1] > 0) {
ndp[1] = std::max(ndp[1], dp[u][0] + dp[v][1]);
if (dp[u][1] > 0) {
ndp[0] = std::max(ndp[0], dp[u][1] + dp[v][1]);
}
}
if (dp[u][1] > 0) {
ndp[1] = std::max(ndp[1], dp[u][1] + dp[v][0]);
}
std::swap(dp[u], ndp);
}
auto ndp = dp[u];
if (c[u] % 2) {
if (dp[u][1] > 0) {
ndp[0] = std::max(ndp[0], dp[u][1] + c[u]);
} else {
ndp[0] = 0;
}
ndp[1] = std::max(ndp[1], dp[u][0] + c[u]);
} else {
if (dp[u][1] > 0) {
ndp[1] = std::max(ndp[1], dp[u][1] + c[u]);
}
ndp[0] = std::max(ndp[0], dp[u][0] + c[u]);
}
std::swap(dp[u], ndp);
assert(dp[u][0] % 2 == 0 && (dp[u][1] % 2 || dp[u][1] == 0));
};
dfs(dfs, 0, -1);
if (dp[0][k % 2] >= k) {
ans++;
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
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: 3880kb
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: 148ms
memory: 3640kb
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 0 0 1 3 3 0 0 2 0 0 2 1 0 0 1 1 0 2 0 1 0 2 1 0 0 0 0 0 1 2 0 0 2 2 0 1 0 0 0 0 3 3 0 0 1 1 2 1 2 0 4 0 1 1 0 1 0 0 1 5 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 3 1 0 1 0 0 4 0 0 0 1 1 0 0 1 0 2 0 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 1 1 1 2 0 1 2 0 0 2 0 0 1 0 0 0 0 0 ...
result:
wrong answer 247th numbers differ - expected: '0', found: '1'