제출 #469355

#제출 시각아이디문제언어결과실행 시간메모리
469355arujbansalCat in a tree (BOI17_catinatree)C++17
100 / 100
161 ms93892 KiB
#include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define rng_seed(x) mt19937 rng(x) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() // #define int long long const int MXN = 2e5 + 5, INF = 1e9 + 5; int N, D; vector<int> g[MXN]; deque<int> dfs(int u, int p) { deque<int> res = {1}; for (const auto &v : g[u]) { if (v == p) continue; deque<int> merge = dfs(v, u); merge.push_front(merge.front()); if (sz(merge) > sz(res)) swap(merge, res); for (int i = 0; i < sz(merge); i++) { int val1 = merge[i] + (D - i < sz(res) ? res[max(i, D - i)] : 0); int val2 = res[i] + (D - i < sz(merge) ? merge[max(i, D - i)] : 0); res[i] = max({res[i], val1, val2}); } for (int i = sz(merge) - 2; i >= 0; i--) res[i] = max(res[i], res[i + 1]); } return res; } void solve() { cin >> N >> D; for (int i = 1; i < N; i++) { int x; cin >> x; g[x].push_back(i); g[i].push_back(x); } cout << dfs(0, -1).front(); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int TC = 1; // cin >> TC; while (TC--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...