# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
851063 | thefless | Cat in a tree (BOI17_catinatree) | C++14 | 1022 ms | 9436 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define task "name"
#define pii pair<ll , int>
#define se second
#define fi first
#define pb push_back
#define BIT(s , i) (((s) >> (i)) & 1)
#define MASK(i) (1LL << (i))
using namespace std;
const int N = 2e3 + 5;
const int MOD = 1e9 + 7;
const ll loo = 1e18;
template<typename T1 , typename T2> bool maximize(T1 &a , T2 b){if(a < b){a = b; return 1;}return 0;}
template<typename T1 , typename T2> bool minimize(T1 &a , T2 b){if(a > b){a = b; return 1;}return 0;}
int n;
int d;
vector<int> adj[N];
struct SUB2{
int dp[1501][1501];
int cnt[1501];
void combine(int u , int v){
for(int i = 0 ; i <= n ; i++) cnt[i] = 0;
for(int i = 0 ; i <= n ; i++){
for(int j = 0 ; j < n ; j++){
if(i + j + 1 < d) maximize(cnt[min(i , j + 1)] , max(dp[u][i] , dp[v][j]));
else maximize(cnt[min(i , j + 1)] , dp[u][i] + dp[v][j]);
}
}
for(int i = 0 ; i <= n ; i++) dp[u][i] = cnt[i];
}
void dfs(int u , int par){
dp[u][0] = 1;
for(auto v : adj[u]){
if(v == par) continue;
dfs(v , u);
combine(u , v);
}
}
}f;
void sub2(){
f.dfs(0 , -1);
cout << *max_element(f.dp[0] , f.dp[0] + n + 1);
}
void solve(){
cin >> n >> d;
for(int i = 1 ; i < n ; i++){
int u;
cin >> u;
adj[u].pb(i);
adj[i].pb(u);
}
if(d >= n) return void(cout << 1);
sub2();
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(task".inp" , "r")){
freopen(task".inp" , "r" , stdin);
freopen(task".out" , "w" , stdout);
}
solve();
}
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... |