Submission #870253

#TimeUsernameProblemLanguageResultExecution timeMemory
870253sleepntsheepCat in a tree (BOI17_catinatree)C++17
0 / 100
1 ms2396 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 200005 int n, d, l[N], h[N], alloc = 1, dp[N][2], t[N]; pair<int, int> g[N<<1]; 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 add_edge(int u, int v) { int p = alloc++; g[p] = {v, h[u]}; h[u] = p; } void dfs(int u, int p = -1, int dep = 1) { l[u] = dep; int base = 0; for (auto j = h[u]; j; j = g[u].second) { int v = g[u].first; if (v == p) continue; dfs(v, u, dep+1); base += dp[v][0] - 1; upd(dp[v][1], 1); } dp[u][0] = base + 1, dp[u][1] = l[u]; for (auto j = h[u]; j; j = g[u].second) { int v = g[u].first; if (v == p) continue; int sel = base + 1 + qry(d - (dp[v][1] - dep) + dep); if (dp[u][0] < sel) { dp[u][0] = sel; dp[u][1] = dp[v][1]; } else if (dp[u][0] == sel && dp[v][1] > dp[u][1]) { dp[u][1] = dp[v][1]; } } for (auto j = h[u]; j; j = g[u].second) { int v = g[u].first; if (v == p) continue; upd(dp[v][1], -1); } } int main() { ShinLena; cin >> n >> d; for (int i = 1, v; i < n; ++i) cin >> v, add_edge(i, v), add_edge(v, i); dfs(1); cout << dp[1][1]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...