QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#560401 | #7684. Sweet Sugar | Okuchiri | WA | 107ms | 30748kb | C++14 | 2.0kb | 2024-09-12 15:32:54 | 2024-09-12 15:32:55 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define int long long
#define mod 998244353
using namespace std;
vector<ll>e[1000010];
ll n,k,ans;
ll a[1000010],f[1000010][2];
void dfs(ll x,ll pre)
{
f[x][0]=0;
f[x][1]=0;
if(a[x]%2==0)f[x][0]=a[x];
else f[x][1]=a[x];
ll s=0;
ll b0=-8000000;
for(auto y:e[x])
{
if(y==pre)continue;
dfs(y,x);
f[x][0]=max(f[x][0],f[y][0]);
f[x][1]=max(f[x][1],f[y][1]);
if(f[y][0]>f[y][1])
{
s+=f[y][0];
if(f[y][1]!=0)b0=max(b0,f[y][1]-f[y][0]);//必须选一个数改变奇偶性
}
else
{
s+=f[y][1];
if(f[y][1]!=0)b0=max(b0,f[y][0]-f[y][1]);//必须选一个数改变奇偶性
}
}
if(a[x]%2==0)
{
if(s%2==0)
{
f[x][0]=max(f[x][0],s+a[x]);
f[x][1]=max(f[x][1],s+b0+a[x]);
}
else
{
f[x][0]=max(f[x][0],s+a[x]+b0);
f[x][1]=max(f[x][1],s+a[x]);
}
}
else
{
if(s%2==0)
{
f[x][1]=max(f[x][1],s+a[x]);
f[x][0]=max(f[x][0],s+a[x]+b0);
}
else
{
f[x][0]=max(f[x][0],s+a[x]+b0);
f[x][1]=max(f[x][1],s+a[x]);
}
}
//cout<<"x:"<<x<<' '<<f[x][0]<<' '<<f[x][1]<<'\n';
if(f[x][k%2]>=k)
{
//cout<<x<<' '<<f[x][1]<<'\n';
f[x][0]=0;
f[x][1]=0;
ans++;
}
}
void work()
{
ans=0;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
e[i].clear();
}
for(int i=1;i<n;i++)
{
ll x,y;
cin>>x>>y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1,0);
cout<<ans<<'\n';
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T=1;
cin>>T;
while(T--)
{
work();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 29716kb
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: 3ms
memory: 30748kb
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: 107ms
memory: 30612kb
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 3 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 0 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 53rd numbers differ - expected: '4', found: '3'