제출 #870595

#제출 시각아이디문제언어결과실행 시간메모리
870595sleepntsheepCat in a tree (BOI17_catinatree)C++17
100 / 100
61 ms24484 KiB
#include <iostream> #include <cstring> #include <vector> #include <algorithm> #include <deque> #include <set> #include <utility> #include <array> #include <cassert> #pragma GCC optimize("O2,unroll-loops") #pragma GCC target("avx2,tune=native") using namespace std; #define ALL(x) x.begin(), x.end() #define ShinLena cin.tie(nullptr)->sync_with_stdio(false); using i64 = long long; #define N 200005 int n, d, t[N]; vector<int> g[N]; array<int, 2> dp[N]; void upd(int p, int k) {;for (; p ; p-=p&-p) t[p] += k; } int qry(int p) { ; int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; } void dfs(int u, int dep) { int base = 0, fs = 1e9; for (auto v : g[u]) { dfs(v, dep+1), base += dp[v][0] - 1; if (dp[v][1] >= dep + (d+1)/2) fs = min(fs, dp[v][1]); } for (auto v : g[u]) upd(dp[v][1], 1); dp[u] = {base + 1 + qry(d + dep), dep}; for (auto v : g[u]) { if (dp[v][1] < dep + (d+1)/2) { upd(dp[v][1], -1); dp[u] = max(dp[u], {base + 1 + qry(dep + max((d+1)/2, d - (dp[v][1] - dep))), dp[v][1]}); upd(dp[v][1], 1); } else { dp[u] = max(dp[u], {base + qry(dep + (d+1)/2), fs}); } } for (auto v : g[u]) upd(dp[v][1], -1); } int main() { ShinLena; cin >> n >> d; for (int i = 1, v; i < n; ++i) cin >> v, g[v].push_back(i); dfs(0, 1); cout << dp[0][0]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...