Submission #535845

#TimeUsernameProblemLanguageResultExecution timeMemory
535845StickfishCat in a tree (BOI17_catinatree)C++17
100 / 100
136 ms21876 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...