Submission #636678

# Submission time Handle Problem Language Result Execution time Memory
636678 2022-08-29T22:34:05 Z tvladm2009 Cat in a tree (BOI17_catinatree) C++14
100 / 100
101 ms 18716 KB
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int MAX_N = 2 * 1e5;
vector<int> g[MAX_N + 1];
int dp[MAX_N + 1], dist[MAX_N + 1];
int n, d;

bool cmp(int x, int y) {
    return dist[x] < dist[y];
}

void dfs(int u, int p = -1) {
    if (g[u].size() == 0 || (g[u].size() == 1 && g[u][0] == p)) {
        dp[u] = 1;
        dist[u] = 0;
        return;
    }
    for (int v : g[u]) {
        if (v != p) {
            dfs(v, u);
        }
    }
    for (int v : g[u]) {
        if (v != p) {
            dp[u] += dp[v];
        }
    }
    sort(g[u].begin(), g[u].end(), cmp);
    int aux = g[u][0];
    if (g[u][0] == p) {
        aux = g[u][1];
    }
    for (int v : g[u]) {
        if (v != aux && v != p && dist[aux] + dist[v] + 2 < d) {
            dp[u]--;
            aux = v;
        }
    }
    dist[u] = dist[aux] + 1;
    if (dist[u] >= d) {
        dp[u]++;
        dist[u] = 0;
    }
}

int main() {
    cin >> n >> d;
    for (int i = 1; i <= n - 1; i++) {
        int x;
        cin >> x;
        g[x + 1].push_back(i + 1);
    }
    dfs(1);
    cout << dp[1];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5000 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 2 ms 4944 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 2 ms 5004 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5000 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 2 ms 4944 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 2 ms 5004 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4976 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 4 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 5004 KB Output is correct
29 Correct 3 ms 5008 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 5008 KB Output is correct
32 Correct 3 ms 5008 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 4 ms 4948 KB Output is correct
35 Correct 4 ms 4948 KB Output is correct
36 Correct 3 ms 5008 KB Output is correct
37 Correct 4 ms 5000 KB Output is correct
38 Correct 4 ms 5012 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 4 ms 5000 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 2 ms 4944 KB Output is correct
12 Correct 2 ms 5004 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 2 ms 5004 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4976 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 4 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 5004 KB Output is correct
29 Correct 3 ms 5008 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 5008 KB Output is correct
32 Correct 3 ms 5008 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 4 ms 4948 KB Output is correct
35 Correct 4 ms 4948 KB Output is correct
36 Correct 3 ms 5008 KB Output is correct
37 Correct 4 ms 5000 KB Output is correct
38 Correct 4 ms 5012 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 4 ms 5076 KB Output is correct
41 Correct 63 ms 11124 KB Output is correct
42 Correct 38 ms 7844 KB Output is correct
43 Correct 41 ms 7860 KB Output is correct
44 Correct 43 ms 7984 KB Output is correct
45 Correct 38 ms 7944 KB Output is correct
46 Correct 90 ms 10936 KB Output is correct
47 Correct 101 ms 10944 KB Output is correct
48 Correct 87 ms 10916 KB Output is correct
49 Correct 87 ms 11044 KB Output is correct
50 Correct 25 ms 6688 KB Output is correct
51 Correct 26 ms 6676 KB Output is correct
52 Correct 27 ms 6732 KB Output is correct
53 Correct 50 ms 8360 KB Output is correct
54 Correct 50 ms 8428 KB Output is correct
55 Correct 50 ms 8412 KB Output is correct
56 Correct 3 ms 5076 KB Output is correct
57 Correct 11 ms 7416 KB Output is correct
58 Correct 40 ms 16200 KB Output is correct
59 Correct 92 ms 18716 KB Output is correct
60 Correct 66 ms 11780 KB Output is correct
61 Correct 75 ms 11528 KB Output is correct