QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#560929 | #7684. Sweet Sugar | SLF666 | WA | 144ms | 30640kb | C++17 | 1.4kb | 2024-09-12 18:59:30 | 2024-09-12 18:59:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
#define PII pair<int,int>
#define endl "\n"
#define ll long long
const int N = 1e6 + 5, M = 5000;
const int mod = 998244353;
vector<int>g[N];
int c[N], f[N][2];
int n,k,ans;
void dfs(int u,int fa){
int sum0 = 0, sum1 = 0;
for(auto &v:g[u]){
if(v == fa)continue;
dfs(v, u);
if((k & 1) && f[v][1] >= k || (k % 2 == 0) && f[v][0] >= k){
ans += 1;
continue;
}
//更新最大奇偶个数
if(f[v][1])sum1 = max(sum1, sum0 + f[v][1]);
if(sum1)sum1 = max(sum1, f[v][0] + sum1);
sum0 = max(sum0, sum0 + f[v][0]);
if(sum1 && f[v][1])sum0 = max(sum0, sum1 + f[v][1]);
}
if(c[u] & 1){
f[u][1] = sum0 + c[u];
if(sum1)f[u][0] = sum1 + c[u];
}
else{
f[u][0] = sum0 + c[u];
if(sum1)f[u][1] = sum1 + c[u];
}
}
void solve(){
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>c[i];
for(int i=1,u,v;i<n;i++){
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
ans = 0;
dfs(1, 0);
//不要忘了特判根节点
if((k & 1) && f[1][1] >= k || (k % 2 == 0) && f[1][0] >= k)ans += 1;
cout<<ans<<endl;
for(int i=1;i<=n;i++){
vector<int>().swap(g[i]);
f[i][0] = f[i][1] = 0;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin>>t;
for(int i=1;i<=t;i++){
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 30640kb
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: 4ms
memory: 30088kb
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: 144ms
memory: 30296kb
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 1 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 0 1 1 0 1 1 0 1 5 0 1 1 1 1 1 1 1 1 1 1 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 1 0 1 1 0 0 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 0 2 2 1 0 2 0 1 1 0 0 0 1 1 ...
result:
wrong answer 3rd numbers differ - expected: '0', found: '1'