QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504625#9101. Zayin and BusIllusionaryDominance#AC ✓48ms9872kbC++201.8kb2024-08-04 14:14:172024-08-04 14:14:17

Judging History

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

  • [2024-08-04 14:14:17]
  • 评测
  • 测评结果:AC
  • 用时:48ms
  • 内存:9872kb
  • [2024-08-04 14:14:17]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

char rr[1 << 22], *csy1, *csy2;
#define GC (csy1 == csy2 && (csy2 = (csy1 = rr) + fread(rr, 1, 1 << 22, stdin), csy1 == csy2) ? EOF : *csy1 ++)

template <class T>
inline void RI(T &t) {
    char c = GC;
    for (t = 0; c < 48 || c > 57; c = GC);
    for ( ; c > 47 && c < 58; c = GC) t = t * 10 + (c ^ 48);
}

const int MAX_N = 100000 + 5;

int N, M, a[MAX_N], fa[MAX_N], son[MAX_N], bro[MAX_N], dep[MAX_N];

inline bool cmp(int i, int j) {return i + a[i] < j + a[j];}

void dfs(int u) {
    M = max(M, dep[u]);
    for (int v = son[u]; v; v = bro[v]) {
        dep[v] = dep[u] + 1; dfs(v);
    }
}

bool check(int mid) {
    static int cnt1[MAX_N], cnt2[MAX_N];
    memset(cnt1, M, sizeof(int) * (M + 1));
    memset(cnt2, M, sizeof(int) * (M + 1));
    for (int i = 1; i <= N; i ++) {
        cnt1[dep[i]] ++;
        int x = mid - i - a[i];
        if (x < 0) return false;
        cnt2[min(x, M)] ++;
    }
    if (cnt1[0] < cnt2[0]) return false;
    for (int i = 1; i <= M; i ++) {
        cnt1[i] += cnt1[i - 1];
        cnt2[i] += cnt2[i - 1];
        if (cnt1[i] < cnt2[i]) return false;
    }
    return true;
}

void solve() {
    memset(son, 0, sizeof(int) * (N + 1));
    
    RI(N);
    for (int i = 2; i <= N; i ++) {
        RI(fa[i]);
        bro[i] = son[fa[i]];
        son[fa[i]] = i;
    }
    M = 0; dfs(1);
    int l = 0x3f3f3f3f, r = 0;
    for (int i = 1; i <= N; i ++) {
        RI(a[i]);
        l = min(l, i + a[i]);
        r = max(r, i + a[i] + N);
    }
    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%d\n", l);
}

int main() {
    int T;
    RI(T);
    while (T --) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3824kb

input:

14
1
1
1
2
1
3
2
1
1 1
2
1
1 2
2
1
2 1
2
1
1 3
2
1
3 1
2
1
1 4
2
1
4 1
3
1 1
1 1 1
3
1 2
1 1 1
3
1 1
1 3 2
3
1 2
1 3 2

output:

2
3
4
3
4
4
5
4
6
5
4
4
6
6

result:

ok 14 lines

Test #2:

score: 0
Accepted
time: 48ms
memory: 9872kb

input:

15
1000
1 2 2 1 1 1 4 8 1 7 7 7 9 3 4 7 15 18 18 4 6 11 19 7 6 1 9 13 2 21 28 6 17 24 24 2 28 5 32 24 23 8 3 26 15 28 25 34 46 41 33 16 46 11 7 2 13 53 12 59 52 53 51 52 31 41 63 18 55 49 55 62 15 19 23 67 18 37 2 4 23 75 58 55 14 84 20 3 7 89 82 15 53 77 60 25 97 69 5 40 54 24 72 10 87 90 99 43 71 ...

output:

98572828
100088663
99870474
100076153
99995412
100076982
99971239
100079684
99928633
100093408
99432584
100093568
99620300
100058966
99565256

result:

ok 15 lines