QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#394993 | #7684. Sweet Sugar | james1BadCreeper | RE | 0ms | 0kb | C++14 | 1.1kb | 2024-04-21 00:04:22 | 2024-04-21 00:04:22 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, k, ans, a[N], fa[N];
int f[N][2], die[N];
vector<int> G[N];
// f[i][3] 代表子树内个数,那么
void dfs(int x, int ff) {
f[x][a[x] & 1] = a[x]; f[x][!(a[x] & 1)] = -1e9;
for (int y : G[x]) if (y != ff) {
dfs(y, x);
if (!die[y]) {
int t0 = f[x][0], t1 = f[x][1];
f[x][0] = max({t0, t0 + f[y][0], t1 + f[y][1]});
f[x][1] = max({t1, t1 + f[y][0], t0 + f[y][1]});
}
}
if (f[x][k & 1] >= k) die[x] = 1, ++ans;
}
void solve(void) {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], G[i].clear(), die[i] = 0;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
G[u].emplace_back(v); G[v].emplace_back(u);
}
ans = 0; dfs(1, 0);
cout << ans << "\n";
}
int main(void) {
freopen("linkcut.in", "r", stdin);
freopen("linkcut.out", "w", stdout);
ios::sync_with_stdio(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
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