Submission #535844

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

const int INF = 1e9;
const int MAXN = 1e5 + 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 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2664 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2660 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2660 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2672 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 3 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 3 ms 2668 KB Output is correct
33 Correct 2 ms 2660 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2668 KB Output is correct
37 Correct 2 ms 2600 KB Output is correct
38 Correct 2 ms 2664 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2664 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2664 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2660 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2660 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2672 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 3 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 3 ms 2668 KB Output is correct
33 Correct 2 ms 2660 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2668 KB Output is correct
37 Correct 2 ms 2600 KB Output is correct
38 Correct 2 ms 2664 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Runtime error 31 ms 10996 KB Execution killed with signal 11
42 Halted 0 ms 0 KB -