Submission #535845

# Submission time Handle Problem Language Result Execution time Memory
535845 2022-03-11T14:12:24 Z Stickfish Cat in a tree (BOI17_catinatree) C++17
100 / 100
136 ms 21876 KB
#include <iostream>
#include <algorithm>
#include <queue>
#include <bitset>
#include <vector>
#include <set>
using namespace std;

const int INF = 1e9;
const int MAXN = 2e5 + 123;
int depth[MAXN];
vector<int> edg[MAXN];
int ans[MAXN];
int mindepth[MAXN];
int sacdepth[MAXN];

int d;

void dfs(int v) {
    //cout << v << endl;
    ans[v] = 1;
    mindepth[v] = depth[v];
    sacdepth[v] = INF;
    for (auto u : edg[v]) {
        depth[u] = depth[v] + 1;
        dfs(u);
        if (mindepth[u] + mindepth[v] - 2 * depth[v] >= d) {
            ans[v] += ans[u];
            sacdepth[v] = min(sacdepth[v], sacdepth[u]);
            mindepth[v] = min(mindepth[v], mindepth[u]);
        } else {
            bool sacv = (mindepth[u] + sacdepth[v] - 2 * depth[v] >= d);
            bool sacu = (mindepth[v] + sacdepth[u] - 2 * depth[v] >= d);
            if (!sacv && !sacu) {
                cout << "ERROR";
                exit(1);
            }
            ans[v] += ans[u] - 1;
            int nmindepth = 0;
            if (sacv) {
                nmindepth = min(mindepth[u], sacdepth[v]);
            }
            if (sacu) {
                nmindepth = max(nmindepth, min(mindepth[v], sacdepth[u]));
            }
            sacdepth[v] = min(sacdepth[v], sacdepth[u]);
            mindepth[v] = nmindepth;
        }
    }
}

signed main() {
    int n;
    cin >> n >> d;
    for (int i = 1; i < n; ++i) {
        int rt;
        cin >> rt;
        edg[rt].push_back(i);
        depth[i] = depth[rt] + 1;
    }
    dfs(0);
    cout << ans[0] << '\n';
}
# 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 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4936 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4948 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 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4936 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 4 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 4 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 3 ms 5068 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 4 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 4 ms 5204 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 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4936 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 4 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 4 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 3 ms 5068 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 3 ms 4948 KB Output is correct
34 Correct 4 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 4 ms 5204 KB Output is correct
41 Correct 78 ms 12012 KB Output is correct
42 Correct 44 ms 8652 KB Output is correct
43 Correct 42 ms 8704 KB Output is correct
44 Correct 45 ms 8764 KB Output is correct
45 Correct 41 ms 8732 KB Output is correct
46 Correct 103 ms 12516 KB Output is correct
47 Correct 136 ms 12544 KB Output is correct
48 Correct 109 ms 12564 KB Output is correct
49 Correct 103 ms 12436 KB Output is correct
50 Correct 29 ms 7496 KB Output is correct
51 Correct 31 ms 7516 KB Output is correct
52 Correct 31 ms 7484 KB Output is correct
53 Correct 60 ms 9904 KB Output is correct
54 Correct 53 ms 9984 KB Output is correct
55 Correct 58 ms 9996 KB Output is correct
56 Correct 4 ms 5076 KB Output is correct
57 Correct 11 ms 7976 KB Output is correct
58 Correct 47 ms 18404 KB Output is correct
59 Correct 122 ms 21876 KB Output is correct
60 Correct 84 ms 13384 KB Output is correct
61 Correct 86 ms 12960 KB Output is correct