Submission #639222

# Submission time Handle Problem Language Result Execution time Memory
639222 2022-09-09T03:12:50 Z BackNoob Cat in a tree (BOI17_catinatree) C++14
51 / 100
780 ms 524288 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()

using namespace std;
const int mxN = 3e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

int n;
int k;
int a[mxN];
int sub[mxN];
int depth[mxN];
int sufmaxroot[mxN];
int sufmaxchild[mxN];
vector<int> dp[mxN];
vector<int> adj[mxN];
int res = 0;

void dfs(int u, int p)
{
    dp[u].resize(1);
    dp[u][0] = 1;
    maximize(res, 1);
    for(int v : adj[u]) {
        if(v == p) continue;
        dfs(v, u);

        vector<int> upd(max(depth[u], depth[v] + 1) + 1, 0);
        for(int j = depth[v]; j >= 0; j--) maximize(upd[j + 1], dp[v][j]);
        for(int i = depth[u]; i >= 0; i--) maximize(upd[i], dp[u][i]);

        sufmaxroot[depth[u]] = dp[u][depth[u]];
        for(int i = depth[u] - 1; i >= 0; i--) sufmaxroot[i] = max(sufmaxroot[i + 1], dp[u][i]);
        sufmaxchild[depth[v]] = dp[v][depth[v]];
        for(int i = depth[v] - 1; i >= 0; i--) sufmaxchild[i] = max(sufmaxchild[i + 1], dp[v][i]);

        for(int i = 0; i <= depth[u]; i++) {
            int j = max(i - 1, k - i - 1);
            if(j <= depth[v]) maximize(upd[i], dp[u][i] + sufmaxchild[j]);
        }

        for(int i = 0; i <= depth[v]; i++) {
            int j = max(i + 1, k - i - 1);
            if(j <= depth[u]) maximize(upd[i + 1], dp[v][i] + sufmaxroot[j]);
        }

        /*
        for(int i = depth[u]; i >= 0; i--) {
            for(int j = depth[v]; j >= 0; j--) {
                if(i + j + 1 >= k)
                    maximize(upd[min(i, j + 1)], dp[u][i] + dp[v][j]);
            }
        }
        */

        maximize(depth[u], depth[v] + 1);
        swap(dp[u], upd);
        //for(int i = 0; i <= depth[u]; i++) dp[u][i] = upd[i];
    }
}

// i
// i + j + 1 > k
// j > k - i - 1
//


// f[u][i] =

void solve()
{
    cin >> n >> k;
    for(int u = 2; u <= n; u++) {
        int v;
        cin >> v;
        ++v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(1, -1);

    for(int i = 0; i <= depth[1]; i++) maximize(res, dp[1][i]);
    cout << res;


}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("task.inp", "r", stdin);

    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14392 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 7 ms 14432 KB Output is correct
11 Correct 7 ms 14388 KB Output is correct
12 Correct 7 ms 14324 KB Output is correct
13 Correct 9 ms 14436 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14392 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 7 ms 14432 KB Output is correct
11 Correct 7 ms 14388 KB Output is correct
12 Correct 7 ms 14324 KB Output is correct
13 Correct 9 ms 14436 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 14 ms 19044 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
23 Correct 7 ms 14380 KB Output is correct
24 Correct 7 ms 14364 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 8 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
29 Correct 9 ms 14508 KB Output is correct
30 Correct 8 ms 14420 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14420 KB Output is correct
33 Correct 7 ms 14536 KB Output is correct
34 Correct 8 ms 14420 KB Output is correct
35 Correct 8 ms 14460 KB Output is correct
36 Correct 8 ms 14448 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 9 ms 14680 KB Output is correct
39 Correct 9 ms 15060 KB Output is correct
40 Correct 12 ms 19004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14392 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 7 ms 14432 KB Output is correct
11 Correct 7 ms 14388 KB Output is correct
12 Correct 7 ms 14324 KB Output is correct
13 Correct 9 ms 14436 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 14 ms 19044 KB Output is correct
22 Correct 8 ms 14420 KB Output is correct
23 Correct 7 ms 14380 KB Output is correct
24 Correct 7 ms 14364 KB Output is correct
25 Correct 8 ms 14420 KB Output is correct
26 Correct 8 ms 14420 KB Output is correct
27 Correct 8 ms 14420 KB Output is correct
28 Correct 8 ms 14420 KB Output is correct
29 Correct 9 ms 14508 KB Output is correct
30 Correct 8 ms 14420 KB Output is correct
31 Correct 8 ms 14420 KB Output is correct
32 Correct 8 ms 14420 KB Output is correct
33 Correct 7 ms 14536 KB Output is correct
34 Correct 8 ms 14420 KB Output is correct
35 Correct 8 ms 14460 KB Output is correct
36 Correct 8 ms 14448 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 9 ms 14680 KB Output is correct
39 Correct 9 ms 15060 KB Output is correct
40 Correct 12 ms 19004 KB Output is correct
41 Correct 71 ms 27152 KB Output is correct
42 Correct 51 ms 21228 KB Output is correct
43 Correct 65 ms 21172 KB Output is correct
44 Correct 56 ms 21260 KB Output is correct
45 Correct 54 ms 21216 KB Output is correct
46 Correct 132 ms 28272 KB Output is correct
47 Correct 127 ms 27984 KB Output is correct
48 Correct 140 ms 28068 KB Output is correct
49 Correct 130 ms 28024 KB Output is correct
50 Correct 53 ms 21196 KB Output is correct
51 Correct 48 ms 21132 KB Output is correct
52 Correct 42 ms 21116 KB Output is correct
53 Correct 94 ms 27996 KB Output is correct
54 Correct 94 ms 28008 KB Output is correct
55 Correct 102 ms 27972 KB Output is correct
56 Correct 14 ms 16832 KB Output is correct
57 Runtime error 780 ms 524288 KB Execution killed with signal 9
58 Halted 0 ms 0 KB -