QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#560383#7684. Sweet SugarOkuchiriWA 120ms30016kbC++142.0kb2024-09-12 15:25:192024-09-12 15:25:20

Judging History

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

  • [2024-09-12 15:25:20]
  • 评测
  • 测评结果:WA
  • 用时:120ms
  • 内存:30016kb
  • [2024-09-12 15:25:19]
  • 提交

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]!=f[y][0])b0=max(b0,f[y][1]-f[y][0]);//必须选一个数改变奇偶性
        }
        else
        {
            s+=f[y][1];
            if(f[y][1]!=f[y][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][1],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)
    {
        f[x][0]=0;
        f[x][1]=0;
        ans++;
    }
}
void work()
{
    ans=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        f[i][0]=0;
        f[i][1]=0;
        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: 29352kb

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: 30016kb

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: 120ms
memory: 29528kb

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
1
0
1
2
0
1
2
2
0
1
0
0
0
0
3
3
0
0
1
1
2
1
2
0
3
0
0
1
0
1
0
0
1
5
0
1
1
1
0
2
1
2
1
1
2
1
1
1
1
0
3
1
1
1
2
2
4
1
0
1
1
1
1
0
1
1
2
2
5
1
1
1
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
2
1
2
0
1
2
0
0
2
0
0
1
0
0
0
0
0
...

result:

wrong answer 29th numbers differ - expected: '0', found: '1'