제출 #870292

#제출 시각아이디문제언어결과실행 시간메모리
870292sleepntsheepCat in a tree (BOI17_catinatree)C++17
51 / 100
29 ms6232 KiB
#include <iostream> #include <cstring> #include <vector> #include <algorithm> #include <deque> #include <set> #include <utility> #include <array> 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 200860 int n, d, l[N], h[N], alloc = 1, dp[N][2], t[N], t2[N]; pair<int, int> g[N]; void upd(int p, int k) { p += 100;for (; p ; p-=p&-p) t[p] += k; } int qry(int p) { p += 100; int z = 0; for (; p < (N<<1); p+=p&-p) z += t[p]; return z; } void add_edge(int u, int v) { g[alloc] = {v, h[u]}; h[u] = alloc++; } void dfs(int u, int p, int dep) { l[u] = dep; int base = 0, lowest = 20868686; for (auto j = h[u]; j; j = g[j].second) { int v = g[j].first; if (v == p) continue; dfs(v, u, dep+1); base += dp[v][0] - 1; lowest = min(lowest, dp[v][1]); } dp[u][0] = base, dp[u][1] = lowest; vector<int> depmap; for (auto j = h[u]; j; j = g[j].second) if (g[j].first != p) upd(dp[g[j].first][1], 1), depmap.emplace_back(dp[g[j].first][1]); sort(ALL(depmap)); { int sel = base + 1 + qry(d + dep); if (dp[u][0] < sel) dp[u][0] = sel, dp[u][1] = dep; else if (dp[u][0] == sel && dep > dp[u][1]) dp[u][1] = dep; } for (auto j = h[u]; j; j = g[j].second) { int v = g[j].first; if (v == p) continue; int delta = dp[v][1] - dep; int safe = (d+1)/2, sel; int subdep = dp[v][1]; if (delta >= safe) { sel = base + qry(dep + safe); auto it = lower_bound(ALL(depmap), dep+safe); subdep = *it; } else sel = base + 1 + qry(dep + max(safe, d- delta)); if (dp[u][0] < sel) dp[u][0] = sel, dp[u][1] = subdep; else if (dp[u][0] == sel && subdep > dp[u][1]) dp[u][1] = subdep; } for (auto j = h[u]; j; j = g[j].second) if (g[j].first != p) upd(dp[g[j].first][1], -1); } int main() { ShinLena; cin >> n >> d; for (int i = 1, v; i < n; ++i) cin >> v, add_edge(v, i); dfs(0, 0, 1); for (int i = 0; i < n; ++i) { // cerr << dp[i][0] << ' '<< dp[i][1] << ' ' <<l[i] <<endl; } cout << dp[0][0]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...