QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#305878#2569. Kill All TermitesckisekiAC ✓17ms10716kbC++201.2kb2024-01-16 05:41:232024-01-16 05:41:24

Judging History

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

  • [2024-01-16 05:41:24]
  • 评测
  • 测评结果:AC
  • 用时:17ms
  • 内存:10716kb
  • [2024-01-16 05:41:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
const int INF = 1 << 20;

int d[N];
vector<int> G[N];

int dp[N][2];

void dfs(int u, int f) {
    int cr = 0;
    for (int v: G[u]) {
        if (v == f) continue;
        dfs(v, u);
        ++cr;
    }
    if (cr == 0) {
        dp[u][0] = 1;
        dp[u][1] = 0;
    } else {
        int cvr = 1, ncvr = 0, mi = INF;
        for (int v: G[u]) {
            if (v == f) continue;
            cvr += min(dp[v][0], dp[v][1]);
            ncvr += dp[v][0];
            dp[u][1] += dp[v][0];
            mi = min(mi, dp[v][1] - dp[v][0]);
        }
        dp[u][0] = min(cvr, ncvr);
        dp[u][1] += mi;
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n; cin >> n;
    if (n <= 2) {
        cout << 1 << '\n';
        return 0;
    }
    for (int i = 2; i <= n; ++i) {
        int u; cin >> u;
        G[u].push_back(i);
        G[i].push_back(u);
        d[i]++;
        d[u]++;
    }

    int r = 1;
    for (int i = 1; i <= n; ++i) {
        if (d[i] != 1) r = i;
    }

    dfs(r, r);

    cout << min(dp[r][0], dp[r][1]) << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1


output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3736kb

input:

2
1

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

8
1 1 2 1 2 3 2

output:

2

result:

ok 1 number(s): "2"

Test #4:

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

input:

3
1 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

4
1 1 1

output:

1

result:

ok 1 number(s): "1"

Test #6:

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

input:

5
1 1 1 1

output:

1

result:

ok 1 number(s): "1"

Test #7:

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

input:

6
1 2 2 2 1

output:

1

result:

ok 1 number(s): "1"

Test #8:

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

input:

7
1 2 1 4 3 4

output:

1

result:

ok 1 number(s): "1"

Test #9:

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

input:

9
1 1 2 3 1 4 2 7

output:

2

result:

ok 1 number(s): "2"

Test #10:

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

input:

10
1 2 3 3 1 2 3 7 9

output:

2

result:

ok 1 number(s): "2"

Test #11:

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

input:

11
1 1 2 1 5 6 7 3 3 2

output:

2

result:

ok 1 number(s): "2"

Test #12:

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

input:

12
1 1 3 3 2 1 4 7 4 8 10

output:

2

result:

ok 1 number(s): "2"

Test #13:

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

input:

13
1 1 3 4 1 4 4 3 6 1 1 4

output:

2

result:

ok 1 number(s): "2"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

14
1 2 3 2 4 1 4 7 3 9 7 8 4

output:

3

result:

ok 1 number(s): "3"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

100
1 2 1 1 3 6 5 5 4 8 4 4 3 14 10 14 8 13 12 18 8 3 7 1 12 10 9 17 26 30 21 11 29 19 20 25 17 10 9 26 6 40 42 29 2 24 14 25 6 41 47 24 21 46 28 8 30 2 19 41 54 43 23 1 65 21 19 35 58 8 71 59 12 2 13 4 16 7 22 58 26 44 1 12 14 80 19 12 43 77 21 54 41 94 37 61 28 82 30

output:

19

result:

ok 1 number(s): "19"

Test #16:

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

input:

100
1 2 3 4 4 6 7 8 9 10 11 10 13 14 15 16 17 17 19 19 15 22 23 15 13 26 27 28 29 30 30 32 28 26 13 36 37 38 39 40 41 42 43 44 45 45 47 48 48 48 51 47 44 54 43 56 56 58 59 60 60 59 63 64 64 66 66 64 69 70 56 72 73 72 75 76 77 42 40 80 40 82 83 83 85 85 85 88 89 88 91 40 38 37 95 96 95 95 2

output:

17

result:

ok 1 number(s): "17"

Test #17:

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

input:

1000
1 2 3 3 5 6 7 8 9 10 11 12 11 14 15 16 16 18 19 19 21 22 23 22 21 26 27 28 29 30 31 28 27 27 35 26 37 38 39 40 41 42 43 44 41 40 47 39 18 50 51 52 53 54 55 56 57 58 59 60 60 58 57 64 65 66 67 66 65 70 71 72 73 72 64 76 76 78 79 80 56 82 18 84 84 86 86 88 88 14 14 92 14 94 94 96 96 98 98 100 101...

output:

169

result:

ok 1 number(s): "169"

Test #18:

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

input:

1000
1 2 2 2 2 2 7 2 2 2 2 2 2 2 15 15 15 2 2 2 21 1 23 23 25 25 23 1 1 1 31 31 31 31 31 31 31 1 39 40 39 42 43 42 42 42 39 39 39 50 50 39 39 54 54 39 57 57 39 60 60 60 60 60 60 39 67 67 69 67 67 67 67 39 75 75 77 77 75 75 75 39 39 39 85 39 39 39 39 39 91 91 93 39 95 95 95 98 98 95 95 95 103 103 105...

output:

152

result:

ok 1 number(s): "152"

Test #19:

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

input:

1000
1 2 3 4 5 6 7 8 7 10 11 11 10 14 6 4 17 17 19 20 3 22 23 23 25 26 23 28 29 30 31 32 33 33 31 36 37 31 3 2 41 42 43 44 42 46 47 48 49 50 51 48 53 54 53 56 57 57 59 60 60 62 63 64 64 66 66 59 69 70 71 70 69 74 75 75 53 78 79 78 78 82 83 53 85 86 86 88 88 88 85 85 85 47 95 96 97 98 99 100 101 100 ...

output:

181

result:

ok 1 number(s): "181"

Test #20:

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

input:

1000
1 2 3 3 5 6 3 8 9 9 3 12 12 12 2 2 17 18 17 20 20 20 17 17 25 17 17 28 2 30 30 32 30 30 35 30 2 38 2 40 41 41 40 40 40 46 47 48 46 50 46 52 46 54 55 54 54 54 2 60 61 62 61 60 60 66 60 60 2 70 71 2 73 73 75 73 77 77 73 80 81 80 83 73 85 85 73 88 88 90 90 88 2 94 95 95 94 98 98 98 101 101 98 104 ...

output:

205

result:

ok 1 number(s): "205"

Test #21:

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

input:

1000
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 28 30 31 32 33 34 35 36 31 38 39 40 41 42 43 44 44 46 47 47 49 50 44 52 44 54 55 56 57 58 59 60 61 62 63 64 65 66 66 68 69 70 71 72 72 72 75 76 77 78 79 77 81 82 76 84 85 85 71 88 71 90 91 91 93 94 94 96 90 98 99 100 101...

output:

165

result:

ok 1 number(s): "165"

Test #22:

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

input:

1000
1 2 2 2 2 2 2 2 1 1 1 1 13 13 13 13 13 13 13 13 13 13 13 1 25 25 1 1 1 1 1 1 1 1 1 1 1 38 38 38 38 38 38 38 38 38 38 38 38 38 38 52 38 38 38 38 38 38 38 38 38 38 38 38 38 66 38 1 1 70 70 70 70 70 70 70 70 70 70 70 70 1 1 1 1 1 1 88 88 1 1 1 1 1 1 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 ...

output:

60

result:

ok 1 number(s): "60"

Test #23:

score: 0
Accepted
time: 17ms
memory: 10360kb

input:

100000
1 2 3 3 5 6 7 8 9 10 11 12 10 14 14 16 17 18 19 19 21 21 23 23 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 39 41 42 41 44 44 46 47 37 49 50 51 49 49 54 55 55 57 58 59 60 60 62 63 64 64 64 62 62 62 70 70 72 62 74 62 76 77 78 79 76 81 82 83 76 76 86 87 88 88 90 88 88 93 62 95 59 59 98 98 100 1...

output:

16927

result:

ok 1 number(s): "16927"

Test #24:

score: 0
Accepted
time: 15ms
memory: 10360kb

input:

100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 17 18 19 20 21 22 23 24 25 20 27 27 29 30 31 30 27 34 34 36 19 38 39 40 41 39 43 44 43 46 46 48 48 48 51 52 53 53 55 56 57 58 59 60 61 62 62 64 65 66 62 68 69 70 71 72 73 73 75 76 77 78 79 77 76 82 83 84 85 84 87 82 82 76 91 92 93 92 95 91 97 98 98 100 1...

output:

16621

result:

ok 1 number(s): "16621"

Test #25:

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

input:

100000
1 2 3 4 5 6 7 8 9 10 11 12 11 14 15 16 17 18 19 20 20 22 23 24 24 26 27 27 29 24 31 32 33 34 35 36 37 36 39 40 40 40 40 44 44 46 47 47 49 49 47 52 46 54 54 56 56 58 59 60 61 62 63 64 64 66 67 68 69 70 71 72 67 74 75 76 77 77 79 80 81 82 83 84 85 85 85 88 89 88 88 92 93 93 92 92 88 98 99 100 1...

output:

16484

result:

ok 1 number(s): "16484"

Test #26:

score: 0
Accepted
time: 16ms
memory: 10384kb

input:

100000
1 2 3 4 5 5 7 8 9 10 11 10 7 14 15 16 16 7 19 20 21 21 23 21 25 21 20 20 29 30 30 32 33 34 34 33 37 38 39 40 38 33 43 44 43 29 47 48 49 50 51 49 49 48 55 56 57 56 59 60 59 56 63 64 64 63 67 68 67 70 71 63 73 63 75 76 77 76 75 80 55 47 83 83 85 85 87 88 89 90 89 83 93 94 47 96 47 98 99 99 47 1...

output:

18227

result:

ok 1 number(s): "18227"

Test #27:

score: 0
Accepted
time: 11ms
memory: 10388kb

input:

100000
1 2 2 4 5 6 7 8 9 10 11 12 13 14 15 16 16 16 19 20 21 22 23 24 25 26 27 28 29 30 31 32 29 34 28 36 37 37 26 40 41 42 42 44 45 46 44 48 49 44 44 40 53 54 55 53 57 58 59 60 60 62 62 64 65 60 67 68 67 67 67 59 73 74 75 76 40 78 40 80 81 82 81 84 84 86 87 88 89 86 91 92 93 94 94 93 97 97 99 93 10...

output:

17563

result:

ok 1 number(s): "17563"

Test #28:

score: 0
Accepted
time: 3ms
memory: 10716kb

input:

100000
1 2 3 3 3 3 3 2 2 2 2 2 2 14 14 14 14 14 14 14 2 2 23 2 25 25 2 2 29 2 2 32 32 32 32 32 2 2 2 40 2 2 43 43 43 2 47 47 2 2 51 51 1 54 54 1 57 57 57 57 1 62 63 62 62 66 62 68 68 62 62 72 62 62 62 76 76 62 79 62 62 62 1 84 84 86 86 86 86 86 86 86 84 84 95 95 95 84 99 99 99 99 99 99 84 106 106 10...

output:

11610

result:

ok 1 number(s): "11610"

Test #29:

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

input:

30
1 1 1 1 2 6 7 8 9 10 10 11 12 13 14 15 15 17 18 19 20 21 22 23 24 25 26 26 26

output:

3

result:

ok 1 number(s): "3"