QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#736341#2411. Balanced CutserverrepairmanWA 2ms9860kbC++172.3kb2024-11-12 10:13:262024-11-12 10:13:26

Judging History

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

  • [2024-11-12 10:13:26]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9860kb
  • [2024-11-12 10:13:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int n, k;
int p[510000], ja[510000][2], root, cnt;
int nhei[510000], hei[510000], mh[100];
int dap[510000];
int dfs(int now){
    int l = ja[now][0];
    int r = ja[now][1];
    hei[now] = 1;
    if(l)
        hei[now] = max(hei[now], dfs(l)+1);
    if(r)
        hei[now] = max(hei[now], dfs(r)+1);
    return hei[now];
}
int chk(int now){
    int ncnt = cnt;
    int phei = 1;
    while(p[now] != -1){
        phei = max(phei+1, nhei[p[now]]);
        int l = ja[p[now]][0];
        int r = ja[p[now]][1];
        if(p[now]>now){
            if(r && phei-2>nhei[r])
                ncnt += mh[phei-2] - mh[nhei[p[now]]-2];
        }
        else if(l && phei>nhei[p[now]]+2)
            return -1;

        now = p[now];
    }
    return ncnt+1;
}
void update(int now){
    nhei[now] = 1;
    while(p[now] != -1){
        nhei[p[now]] = max(nhei[now]+1, nhei[p[now]]);
        int l = ja[p[now]][0];
        int r = ja[p[now]][1];
        if(p[now]>now && r)
            nhei[r] = max(nhei[r], nhei[p[now]]-2);
        now = p[now];
    }
}
void solve(int now){
    int l = ja[now][0];
    int r = ja[now][1];
    if(nhei[now]){
        // Propagation
        if(nhei[now]>1){
            if(!l || hei[l]<nhei[now]-1){
                if(l)
                    nhei[l] = nhei[now]-2;
                nhei[r] = nhei[now]-1;
            }
            else{
                if(r)
                    nhei[r] = nhei[now]-2;
                nhei[l] = nhei[l]-1;
            }
        }
        
        dap[now] = 1;
    }
    else{
        int kk = chk(now);
        if(kk<=k && kk>0){
            dap[now] = 1;
            update(now);
            cnt = kk;
        }
    }
    if(l)
        solve(l);
    if(r)
        solve(r);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k;
    for(int i=1;i<=n;i++)
        cin >> p[i];
    for(int i=1;i<=n;i++){
        if(p[i]==-1)
            root = i;
        else
            ja[p[i]][p[i]<i] = i;
    }

    mh[1] = 1;
    mh[2] = 2;
    for(int i=3;i<=40;i++)
        mh[i] = mh[i-1] + mh[i-2] + 1;
    
    dfs(root);
    solve(root);

    for(int i=1;i<=n;i++)
        cout << dap[i];
    cout << "\n";

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 9800kb

input:

2 1
-1
1

output:

10

result:

ok correct

Test #2:

score: 0
Accepted
time: 1ms
memory: 9860kb

input:

2 1
2
-1

output:

01

result:

ok correct

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 9812kb

input:

24 16
2
4
2
8
6
4
6
16
10
12
10
8
14
12
14
-1
18
19
21
19
16
23
21
23

output:

111111110101010100101010

result:

wrong answer Not the right number of nodes.