QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#374930#7684. Sweet Sugararnold518WA 158ms57812kbC++173.4kb2024-04-02 19:37:512024-04-02 19:37:51

Judging History

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

  • [2024-04-02 19:37:51]
  • 评测
  • 测评结果:WA
  • 用时:158ms
  • 内存:57812kb
  • [2024-04-02 19:37:51]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e6;

int TC, N, K;
int A[MAXN+10];
vector<int> adj[MAXN+10];

int dp[MAXN+10], dp2[MAXN+10], sum[MAXN+10], sz[MAXN+10], ans;
vector<int> V[MAXN+10];
bool dfs(int now, int bef)
{
    int cnt=0;
    for(auto nxt : adj[now])
    {
        if(nxt==bef) continue;
        if(dfs(nxt, now)) continue;

        cnt++;
        if(cnt==1) swap(V[now], V[nxt]), dp2[now]=dp2[nxt];
        int t=dp[now]+dp[nxt];
        if(dp[now]!=sz[now]) t=min(t, dp[now]);
        if(dp[nxt]!=sz[nxt]) t=min(t, dp[nxt]);
        dp[now]=t;

        sum[now]+=sum[nxt];
        sz[now]+=sz[nxt];
    }
    sum[now]+=A[now];
    sz[now]+=(A[now]!=0);
    if(cnt>1)
    {
        V[now].clear();

        if(sum[now]>=K)
        {
            bool flag=true;
            if(A[now]==0)
            {
                if(K%2 && K/2<dp[now]) flag=false;
            }
            else if(A[now]==2)
            {
                if(K%2 && K/2<dp[now]+1) flag=false;
            }
            else if(A[now]==1)
            {
                if(K%2==0 && K/2<=dp[now]) flag=false;
            }
            if(flag || sum[now]==K) { ans++; return true; }
        }

        if(A[now]==2) dp[now]++;
        else if(A[now]==1) dp[now]=0;
        dp2[now]=dp[now];
    }
    else
    {
        int t=dp2[now];
        if(!V[now].empty()) t=V[now].back();
        if(A[now]==1) V[now].push_back(t);
        else if(A[now]==2) V[now].push_back(t+1);

        // printf("%d : ", now);
        // for(auto it : V[now]) printf("%d ", it);
        // printf(" : %d %d\n", dp[now], dp2[now]);

        if(sum[now]>=K)
        {
            if(V[now].empty())
            {
                if(dp2[now]*2<=K) { ans++; return true; }
                else if(K%2==0) { ans++; return true; }
            }
            else if(V[now].size()+dp2[now]+V[now].back()<=K) { ans++; return true; }
            else if(V[now].back()-dp2[now]+V[now].size()<=K)
            {
                if((V[now].back()-dp2[now]+V[now].size()+K)%2==0) { ans++; return true; }
            }
            else
            {
                //for(int mid=0; mid<V[now].size(); mid++) printf("%d ", V[now].size()-1-mid+V[now].back()-V[now][mid]); printf("!\n");
                int lo=-1, hi=V[now].size()-1;
                while(lo+1<hi)
                {
                    int mid=lo+hi>>1;
                    if(V[now].size()-1-mid+V[now].back()-V[now][mid]<=K) hi=mid;
                    else lo=mid;
                }
                if(V[now].size()-1-hi+V[now].back()-V[now][hi]==K) { ans++; return true; }
            }
        }

        if(A[now]==2) dp[now]++;
        else if(A[now]==1) dp[now]=0;
    }
    return false;
}

int main()
{
    scanf("%d", &TC);
    while(TC--)
    {
        scanf("%d%d", &N, &K);
        for(int i=1; i<=N; i++) scanf("%d", &A[i]);
        for(int i=1; i<N; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        dfs(1, 1);
        printf("%d\n", ans);

        for(int i=1; i<=N; i++)
        {
            dp[i]=sum[i]=sz[i]=dp2[i]=0;
            adj[i].clear();
            V[i].clear();
        }
        ans=0;
    }
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 57812kb

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

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: 158ms
memory: 57408kb

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 273rd numbers differ - expected: '0', found: '1'