Submission #941969

#TimeUsernameProblemLanguageResultExecution timeMemory
941969andrei_c1Cat in a tree (BOI17_catinatree)C++17
51 / 100
12 ms11160 KiB
#include <bits/stdc++.h> using namespace std; const int kN = 1e5; const int kLog = 19; const int kInf = 1e9; int dep[kN], tin[kN]; vector<int> adj[kN], rmq[kLog]; void dfs(int u) { tin[u] = rmq[0].size(); rmq[0].emplace_back(u); for(const auto &v: adj[u]) { dfs(v); rmq[0].emplace_back(u); } } int minNode(int u, int v) { if(dep[u] < dep[v]) return u; return v; } int lca(int u, int v) { int l = tin[u], r = tin[v]; if(l > r) swap(l, r); int lg = __lg(r - l + 1); return minNode(rmq[lg][l], rmq[lg][r - (1 << lg) + 1]); } int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } void minSelf(int &x, int y) { if(y < x) { x = y; } } int findClosest(int u, const vector<int> &nodes) { int res = kInf; for(const auto &v: nodes) { minSelf(res, dist(u, v)); } return res; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int n, d; cin >> n >> d; for(int i = 1; i < n; i++) { int x; cin >> x; adj[x].emplace_back(i); dep[i] = dep[x] + 1; } rmq[0].reserve(2 * n); dfs(0); for(int i = 1; (1 << i) < 2 * n; i++) { for(int j = 0; j + (1 << i) - 1 < 2 * n - 1; j++) { rmq[i].emplace_back(minNode(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))])); } } vector<int> ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&] (int u, int v) { return dep[u] > dep[v]; }); vector<int> nodes; for(const auto &u: ord) { int closest = findClosest(u, nodes); if(closest >= d) { nodes.emplace_back(u); } } cout << nodes.size() << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...