# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1127390 | kh0i | Cat in a tree (BOI17_catinatree) | C++20 | 75 ms | 28484 KiB |
#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
struct Deque{
vector<int> a;
int size(){ return sz(a); }
void push_front(int x){
a.push_back(x);
}
int& operator [] (int x){
return a.rbegin()[x];
}
void clear(){
vector<int>().swap(a);
}
};
const int N = 2e5 + 3;
int n, d;
vector<int> g[N];
Deque f[N];
void dfs(int u){
f[u].push_front(1);
for(int v : g[u]){
dfs(v);
f[v].push_front(f[v][0]);
if(sz(f[v]) > sz(f[u]))
swap(f[u], f[v]);
for(int i = 0; i < sz(f[v]); ++i){
int t = max(i, d - i);
int fi = (t < sz(f[v]) ? f[v][t] + f[u][i] : f[u][i]);
int se = (t < sz(f[u]) ? f[u][t] + f[v][i] : f[u][i]);
f[u][i] = max(fi, se);
}
for(int i = sz(f[v]) - 1; i >= 0; --i){
if(i + 1 < sz(f[u]))
f[u][i] = max(f[u][i], f[u][i + 1]);
}
f[v].clear();
}
}
void solve(){
cin >> n >> d;
for(int i = 1; i < n; ++i){
int p;
cin >> p;
g[p].push_back(i);
}
dfs(0);
cout << f[0][0];
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |