Submission #649178

#TimeUsernameProblemLanguageResultExecution timeMemory
649178LeonaRagingCat in a tree (BOI17_catinatree)C++14
100 / 100
338 ms36296 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define pb push_back #define db(val) "[" #val " = " << (val) << "] " const ll mod = 1e9 + 7; const int maxn = 2e5 + 4; const int INF = 1e9; const int N = 18; int n, d, h[maxn], par[maxn], mn[maxn], dis[maxn][20], cen_dep[maxn]; vector<int> adj[maxn], nodes; bool used[maxn]; void dfs(int u, int p) { for (int v : adj[u]) if (v != p) { h[v] = h[u] + 1; dfs(v, u); } } void Solve(int u, int p, int dep, int c) { dis[u][c] = dep; for (int v : adj[u]) if (v != p && !used[v]) Solve(v, u, dep + 1, c); } struct Centroid_Decomposition { int sz[maxn]; int dfs(int u, int p) { sz[u] = 1; for (int v : adj[u]) if (v != p && !used[v]) sz[u] += dfs(v, u); return sz[u]; } int Centroid(int u, int p, int tot) { for (int v : adj[u]) if (v != p && !used[v] && sz[v] > tot / 2) return Centroid(v, u, tot); return u; } void build(int u, int p, int dep) { int c = Centroid(u, 0, dfs(u, 0)); cen_dep[c] = dep; Solve(c, 0, 0, dep); used[c] = 1; par[c] = p; for (int v : adj[c]) if (!used[v]) build(v, c, dep + 1); } } Cen; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen(".INP", "r", stdin); //freopen(".OUT", "w", stdout); cin >> n >> d; for (int u = 2; u <= n; u++) { int v; cin >> v; v++; adj[u].pb(v); adj[v].pb(u); } dfs(1, 0); Cen.build(1, 0, 0); for (int u = 1; u <= n; u++) nodes.pb(u); sort(nodes.begin(), nodes.end(),[&](int u, int v) { return h[u] > h[v]; }); int res = 0; memset(mn, 63, sizeof mn); for (int u : nodes) { int dep = mn[0], v = u; while (v) { dep = min(dep, dis[u][cen_dep[v]] + mn[v]); v = par[v]; } if (dep < d) continue; res++; v = u; while (v) { mn[v] = min(mn[v], dis[u][cen_dep[v]]); v = par[v]; } } cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...