QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865647#5124. TreeayxrakzilWA 199ms89532kbC++142.0kb2025-01-21 20:45:262025-01-21 20:45:28

Judging History

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

  • [2025-01-21 20:45:28]
  • 评测
  • 测评结果:WA
  • 用时:199ms
  • 内存:89532kb
  • [2025-01-21 20:45:26]
  • 提交

answer

#include <bits/stdc++.h>

const int N = 1e6 + 5, M = 1e3 + 5;

int q, n, x;
int head[N], cnt;
struct edge{ 
    int nxt, to;
} e[N];
int w[N], hson[N], d[N];
int a[M], m;

int read() {
    int res = 0, i = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') i = -i;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = getchar();
    }
    return res * i;
}

void write(int x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

void add(int u, int v) {
    e[++cnt].to = v;
    e[cnt].nxt = head[u];
    head[u] = cnt;
}

void dfs1(int k, int dep) {
    w[k] = 0, d[k] = dep, hson[k] = 0;
    if (!head[k]) {
        w[k] = dep;
        return;
    }
    for (int i = head[k]; i; i = e[i].nxt) {
        dfs1(e[i].to, dep + 1);
        w[k] = std::max(w[k], w[e[i].to]);
        if (w[k] == w[e[i].to])
            hson[k] = e[i].to;
    }
}

void dfs2(int k, int top) {
    if (!head[k]) {
        a[++m] = d[k] - d[top] + 1;
        return;
    }
    if (hson[k]) dfs2(hson[k], top);
    for (int i = head[k]; i; i = e[i].nxt)
        if (e[i].to != hson[k])
            dfs2(e[i].to, e[i].to);
}

void Main() {
    n = read();
    for (int i = 1; i <= n; ++i) head[i] = 0;
    cnt = 0;
    for (int i = 2; i <= n; ++i) {
        x = read();
        add(x, i);
    }
    m = 0;
    dfs1(1, 1);
    // for (int i = 1; i <= n; ++i) write(d[i]), putchar(' ');
    // puts("");
    // for (int i = 1; i <= n; ++i) write(w[i]), putchar(' ');
    // puts("");
    dfs2(1, 1);
    std::sort(a + 1, a + m + 1, [](int x, int y) {return x > y;});
    // for (int i = 0; i <= m; ++i) write(a[i]), putchar(' ');
    // puts("");
    int ans = m;
    for (int i = 0, sum = 0; i < m; ++i, ++sum)
        ans = std::min(ans, sum + a[i + 1]);
    write(ans), puts("");
}

int main() {
    q = read();
    while (q--) Main();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
7
1 1 2 2 2 3
5
1 2 3 4

output:

3
1

result:

ok 2 number(s): "3 1"

Test #2:

score: 0
Accepted
time: 8ms
memory: 11856kb

input:

46234
1

2
1
3
1 1
4
1 1 1
5
1 1 1 1
6
1 1 1 1 1
7
1 1 1 1 1 1
8
1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 2
9
1 1 1 1 1 1 1 3
9
1 1 1 1 1 1 1 4
9
1 1 1 1 1 1 1 5
9
1 1 1 1 1 1 1 6
9
1 1 1 1 1 1 1 7
9
1 1 1 1 1 1 1 8
8
1 1 1 1 1 1 2
9
1 1 1 1 1 1 2 1
9
1 1 1 1 1 1 2 2
9
1 1 1 1 1 1 2 3
9
1 1 1...

output:

1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
2
2
2
3
2
3
3
3
3
2
2
2
3
3
2
3
3
3
2
2
2
3
3
3
2
3
3
2
2
2
3
3
3
3
2
3
2
2
2
3
3
3
3
3
2
2
2
2
2
2
3
3
3
3
2
3
2
2
2
3
3
3
3
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
2
2
2
3
3
3
3
2
2
2
2
2
3
2
3
3
3
2
3
3
3
3
3
3
3
...

result:

ok 46234 numbers

Test #3:

score: 0
Accepted
time: 57ms
memory: 89532kb

input:

1
1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 25ms
memory: 27012kb

input:

1
1000000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: 0
Accepted
time: 67ms
memory: 27092kb

input:

1
1000000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

1000

result:

ok 1 number(s): "1000"

Test #6:

score: -100
Wrong Answer
time: 199ms
memory: 27152kb

input:

1
1000000
1 1 1 2 3 4 5 6 7 3 6 8 9 10 13 11 12 13 14 15 8 16 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 51 39 54 55 56 57 58 59 60 61 68 62 63 64 65 66 67 68 69 70 71 59 72 73 74 75 76 77 78 79 80 81 82 32 23 83 84 85 86 87 88 8...

output:

1404

result:

wrong answer 1st numbers differ - expected: '1405', found: '1404'