Submission #528701

# Submission time Handle Problem Language Result Execution time Memory
528701 2022-02-21T06:44:08 Z Alex_tz307 Cat in a tree (BOI17_catinatree) C++17
100 / 100
505 ms 18724 KB
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

void minSelf(int &x, int y) {
  if (y < x) {
    x = y;
  }
}

struct ST {
  int n;
  vector<int> t;

  ST(int N) : n(N) {
    int dim = 1;
    while (dim < n) {
      dim *= 2;
    }
    t.resize(dim * 2, INF);
  }

  void update(int x, int lx, int rx, int st, int dr, int v) {
    if (st <= lx && rx <= dr) {
      minSelf(t[x], v);
      return;
    }
    int mid = (lx + rx) / 2;
    if (st <= mid) {
      update(x * 2, lx, mid, st, dr, v);
    }
    if (mid < dr) {
      update(x * 2 + 1, mid + 1, rx, st, dr, v);
    }
  }

  void update(int st, int dr, int v) {
    update(1, 1, n, st, dr, v);
  }

  int query(int x, int lx, int rx, int pos) {
    if (lx == rx) {
      return t[x];
    }
    int mid = (lx + rx) / 2;
    if (pos <= mid) {
      return min(t[x], query(x * 2, lx, mid, pos));
    }
    return min(t[x], query(x * 2 + 1, mid + 1, rx, pos));
  }

  int query(int pos) {
    return query(1, 1, n, pos);
  }
};

const int kN = 2e5;
vector<int> g[kN];
int timer, tin[kN], tout[kN];

void dfs(int u) {
  tin[u] = ++timer;
  for (int v : g[u]) {
    dfs(v);
  }
  tout[u] = timer;
}

void testCase() {
  int n, d;
  cin >> n >> d;
  vector<int> dep(n), par(n, -1);
  vector<pair<int, int>> nodes(n, {0, 0});
  for (int v = 1; v < n; ++v) {
    int u;
    cin >> u;
    g[u].emplace_back(v);
    dep[v] = dep[u] + 1;
    par[v] = u;
    nodes[v] = {dep[v], v};
  }
  dfs(0);
  sort(nodes.begin(), nodes.end(), greater<pair<int, int>>());
  ST t(n);
  int ans = 0;
  for (auto it : nodes) {
    if (t.query(tin[it.second]) < d - dep[it.second]) {
      continue;
    }
    ans += 1;
    for (int v = it.second; v != -1; v = par[v]) {
      t.update(tin[v], tout[v], dep[it.second] - 2 * dep[v]);
    }
  }
  cout << ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 5 ms 5020 KB Output is correct
6 Correct 3 ms 4952 KB Output is correct
7 Correct 4 ms 5016 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 4 ms 5016 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5020 KB Output is correct
15 Correct 3 ms 5016 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 5020 KB Output is correct
20 Correct 3 ms 5020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 5 ms 5020 KB Output is correct
6 Correct 3 ms 4952 KB Output is correct
7 Correct 4 ms 5016 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 4 ms 5016 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5020 KB Output is correct
15 Correct 3 ms 5016 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 5020 KB Output is correct
20 Correct 3 ms 5020 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5024 KB Output is correct
23 Correct 3 ms 4940 KB Output is correct
24 Correct 3 ms 5016 KB Output is correct
25 Correct 7 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 4 ms 5072 KB Output is correct
33 Correct 4 ms 5020 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 4 ms 5072 KB Output is correct
36 Correct 5 ms 5072 KB Output is correct
37 Correct 4 ms 5072 KB Output is correct
38 Correct 4 ms 5052 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 24 ms 5172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 5 ms 5020 KB Output is correct
6 Correct 3 ms 4952 KB Output is correct
7 Correct 4 ms 5016 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 2 ms 4940 KB Output is correct
10 Correct 4 ms 5016 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5020 KB Output is correct
15 Correct 3 ms 5016 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 5020 KB Output is correct
20 Correct 3 ms 5020 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5024 KB Output is correct
23 Correct 3 ms 4940 KB Output is correct
24 Correct 3 ms 5016 KB Output is correct
25 Correct 7 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 3 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 3 ms 5072 KB Output is correct
32 Correct 4 ms 5072 KB Output is correct
33 Correct 4 ms 5020 KB Output is correct
34 Correct 3 ms 5072 KB Output is correct
35 Correct 4 ms 5072 KB Output is correct
36 Correct 5 ms 5072 KB Output is correct
37 Correct 4 ms 5072 KB Output is correct
38 Correct 4 ms 5052 KB Output is correct
39 Correct 3 ms 5072 KB Output is correct
40 Correct 24 ms 5172 KB Output is correct
41 Correct 90 ms 16292 KB Output is correct
42 Correct 245 ms 10576 KB Output is correct
43 Correct 59 ms 10508 KB Output is correct
44 Correct 48 ms 10536 KB Output is correct
45 Correct 49 ms 10568 KB Output is correct
46 Correct 505 ms 16076 KB Output is correct
47 Correct 113 ms 16164 KB Output is correct
48 Correct 117 ms 16068 KB Output is correct
49 Correct 113 ms 16072 KB Output is correct
50 Correct 41 ms 9288 KB Output is correct
51 Correct 33 ms 9380 KB Output is correct
52 Correct 30 ms 9252 KB Output is correct
53 Correct 66 ms 13664 KB Output is correct
54 Correct 69 ms 13636 KB Output is correct
55 Correct 64 ms 13556 KB Output is correct
56 Correct 33 ms 5072 KB Output is correct
57 Correct 24 ms 7028 KB Output is correct
58 Correct 100 ms 14052 KB Output is correct
59 Correct 182 ms 18724 KB Output is correct
60 Correct 95 ms 16940 KB Output is correct
61 Correct 102 ms 16576 KB Output is correct