/* [Author : Hoang Duy Vu] */
#include <bits/stdc++.h>
#define All(x) (x).begin(),(x).end()
#define ll long long
#define C make_pair
#define fi first
#define se second
#define two second.first
#define thr second.second
#define TASK "txt"
using namespace std;
template<typename T> bool maximize(T &res, const T &val) {
if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) {
if (res > val) { res = val; return true; } return false; }
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int LOG = 20;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;
const int mod = 1e9 + 7;
const int N = 200010;
vector <int> a[N];
int dp[N][450] = {0};
int t[N] = {0};
int f[1100] = {0};
int high[N] = {0};
int n , k;
int sz[N] = {0};
int ans = 0;
void input()
{
cin >> n >> k;
for (int i = 1 ; i < n ; i++)
{
int x;
cin >> x;
a[x].push_back(i);
a[i].push_back(x);
}
}
void dfs(int u ,int par)
{
memset(dp[u],0,sizeof(dp[u]));
for (int v : a[u])
if (v != par)
{
dfs(v,u);
maximize(high[u],high[v] + 1);
for (int i = 0 ; i <= k ; i++) f[i] = dp[u][i];
for (int i = 0 ; i <= min(high[u],k) ; i++)
{
for (int j = max(i - 1,0) ; j <= min(high[v],k) ; j++)
{
int x = j + 1;
maximize(dp[u][i],f[max(i,k - x)] + dp[v][j]);
}
}
}
maximize(dp[u][0],dp[u][k] + 1);
}
void case1()
{
dfs(0,0);
int res = 0;
for (int i = 0 ; i <= high[0] ; i++) maximize(res,dp[0][i]);
cout << res;
}
void DFS(int u , int par)
{
memset(dp[u],-1,sizeof(dp[u]));
dp[u][0] = INF;
sz[u] = 1;
for (int v : a[u])
if (v != par)
{
DFS(v,u);
sz[u] = min(ans,sz[u] + sz[v]);
// cout << u << " " << v << " " << ans << " " << sz[u] << " " << sz[v] << "\n";
for (int i = 0 ; i <= ans ; i++) f[i] = dp[u][i];
for (int i = 1 ; i <= sz[u] ; i++)
{
for (int z = 0 ; z <= i ; z++)
if (f[z] != -1 && dp[v][i - z] != -1 && f[z] + dp[v][i - z] + 1 >= k)
maximize(dp[u][i],min(f[z],dp[v][i - z] + 1));
}
}
for (int i = 0 ; i <= sz[u] ; i++) if (dp[u][i] >= k) maximize(dp[u][i + 1],0);
// cout << u << " " << sz[u] << " :";
// for (int i = 0 ; i <= sz[u] ; i++) cout << dp[u][i] << " ";
// cout << "\n";
}
void case2()
{
DFS(0,0);
int res = 0;
for (int i = 0 ; i <= sz[0] ; i++)
if (dp[0][i] != -1) res = i;
cout << res;
}
void solve()
{
ans = (n / k) + 1;
if (k < ans) case1();
else case2();
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
if(fopen(TASK".inp", "r")){
freopen(TASK".inp","r",stdin);
freopen(TASK".out","w",stdout);
}
int tk;
tk = 1;
//cin >> tk;
while (tk--)
{
input();
solve();
}
return 0;
}
Compilation message
catinatree.cpp: In function 'int main()':
catinatree.cpp:137:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(TASK".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:138:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(TASK".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
7892 KB |
Output is correct |
22 |
Correct |
4 ms |
5844 KB |
Output is correct |
23 |
Correct |
3 ms |
5844 KB |
Output is correct |
24 |
Correct |
3 ms |
5844 KB |
Output is correct |
25 |
Correct |
3 ms |
6740 KB |
Output is correct |
26 |
Correct |
4 ms |
6740 KB |
Output is correct |
27 |
Correct |
4 ms |
6740 KB |
Output is correct |
28 |
Correct |
4 ms |
7636 KB |
Output is correct |
29 |
Correct |
4 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7636 KB |
Output is correct |
31 |
Correct |
4 ms |
7636 KB |
Output is correct |
32 |
Correct |
4 ms |
7636 KB |
Output is correct |
33 |
Correct |
4 ms |
7636 KB |
Output is correct |
34 |
Correct |
4 ms |
7508 KB |
Output is correct |
35 |
Correct |
4 ms |
7508 KB |
Output is correct |
36 |
Correct |
5 ms |
7636 KB |
Output is correct |
37 |
Correct |
4 ms |
7636 KB |
Output is correct |
38 |
Correct |
5 ms |
7764 KB |
Output is correct |
39 |
Correct |
5 ms |
7764 KB |
Output is correct |
40 |
Correct |
4 ms |
7764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
7892 KB |
Output is correct |
22 |
Correct |
4 ms |
5844 KB |
Output is correct |
23 |
Correct |
3 ms |
5844 KB |
Output is correct |
24 |
Correct |
3 ms |
5844 KB |
Output is correct |
25 |
Correct |
3 ms |
6740 KB |
Output is correct |
26 |
Correct |
4 ms |
6740 KB |
Output is correct |
27 |
Correct |
4 ms |
6740 KB |
Output is correct |
28 |
Correct |
4 ms |
7636 KB |
Output is correct |
29 |
Correct |
4 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7636 KB |
Output is correct |
31 |
Correct |
4 ms |
7636 KB |
Output is correct |
32 |
Correct |
4 ms |
7636 KB |
Output is correct |
33 |
Correct |
4 ms |
7636 KB |
Output is correct |
34 |
Correct |
4 ms |
7508 KB |
Output is correct |
35 |
Correct |
4 ms |
7508 KB |
Output is correct |
36 |
Correct |
5 ms |
7636 KB |
Output is correct |
37 |
Correct |
4 ms |
7636 KB |
Output is correct |
38 |
Correct |
5 ms |
7764 KB |
Output is correct |
39 |
Correct |
5 ms |
7764 KB |
Output is correct |
40 |
Correct |
4 ms |
7764 KB |
Output is correct |
41 |
Correct |
199 ms |
346440 KB |
Output is correct |
42 |
Correct |
140 ms |
185304 KB |
Output is correct |
43 |
Correct |
167 ms |
185272 KB |
Output is correct |
44 |
Correct |
144 ms |
185268 KB |
Output is correct |
45 |
Correct |
145 ms |
185312 KB |
Output is correct |
46 |
Correct |
290 ms |
365696 KB |
Output is correct |
47 |
Correct |
299 ms |
365644 KB |
Output is correct |
48 |
Correct |
293 ms |
365620 KB |
Output is correct |
49 |
Correct |
308 ms |
365732 KB |
Output is correct |
50 |
Correct |
144 ms |
185204 KB |
Output is correct |
51 |
Correct |
131 ms |
185208 KB |
Output is correct |
52 |
Correct |
133 ms |
185100 KB |
Output is correct |
53 |
Correct |
257 ms |
365456 KB |
Output is correct |
54 |
Correct |
261 ms |
365260 KB |
Output is correct |
55 |
Correct |
263 ms |
365340 KB |
Output is correct |
56 |
Correct |
5 ms |
9428 KB |
Output is correct |
57 |
Correct |
31 ms |
45296 KB |
Output is correct |
58 |
Correct |
121 ms |
195016 KB |
Output is correct |
59 |
Correct |
321 ms |
376528 KB |
Output is correct |
60 |
Correct |
215 ms |
356616 KB |
Output is correct |
61 |
Correct |
528 ms |
347416 KB |
Output is correct |