Submission #639439

# Submission time Handle Problem Language Result Execution time Memory
639439 2022-09-10T00:40:53 Z BackNoob Cat in a tree (BOI17_catinatree) C++14
100 / 100
208 ms 163628 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 res = 0;
vector<int> adj[mxN];

deque<ll> dfs(int u, int p)
{
    deque<ll> res;
    res.push_front(1);
    for(int v : adj[u]) {
        if(v == p) continue;
        deque<ll> add = dfs(v, u);
        add.push_front(add.front());
        if(sz(res) < sz(add)) swap(res, add);

        deque<ll> combined = add;
        for(int x = 0; x < sz(add); x++) {
            int y = max(x, k - x);
            if(y < sz(res))
                maximize(combined[x], add[x] + res[y]);
            if(y < sz(add))
                maximize(combined[x], res[x] + add[y]);
        }
        int maximum = 0;
        for(int i = sz(add) - 1; i >= 0; i--) {
            maximize(maximum, combined[i]);
            maximize(maximum, add[i]);
            maximize(res[i], maximum);
        }
    }

    return res;
}

// dp[u][i] = dp[u][i + 1]
// dp[u][min(i, j + 1)] = dp[u][i] + dp[v][j];
// dp[u][i] = dp[u][i] + sufchild[v][j]
// dp[u][j + 1] = sufroot[u][i] + dp[v][j + 1]

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

    deque<ll> res = dfs(1, -1);
    cout << res.front();
}

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 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7388 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7376 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7384 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 5 ms 7336 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7380 KB Output is correct
17 Correct 6 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7372 KB Output is correct
20 Correct 4 ms 7288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7388 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7376 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7384 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 5 ms 7336 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7380 KB Output is correct
17 Correct 6 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7372 KB Output is correct
20 Correct 4 ms 7288 KB Output is correct
21 Correct 5 ms 9556 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7400 KB Output is correct
24 Correct 4 ms 7344 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 5 ms 7380 KB Output is correct
28 Correct 4 ms 7404 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 5 ms 7380 KB Output is correct
31 Correct 4 ms 7380 KB Output is correct
32 Correct 4 ms 7400 KB Output is correct
33 Correct 5 ms 7380 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 ms 7384 KB Output is correct
36 Correct 6 ms 7508 KB Output is correct
37 Correct 4 ms 7508 KB Output is correct
38 Correct 5 ms 7764 KB Output is correct
39 Correct 5 ms 8152 KB Output is correct
40 Correct 6 ms 9556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7388 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7376 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7384 KB Output is correct
13 Correct 5 ms 7380 KB Output is correct
14 Correct 5 ms 7336 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7380 KB Output is correct
17 Correct 6 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7372 KB Output is correct
20 Correct 4 ms 7288 KB Output is correct
21 Correct 5 ms 9556 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7400 KB Output is correct
24 Correct 4 ms 7344 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 5 ms 7380 KB Output is correct
28 Correct 4 ms 7404 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 5 ms 7380 KB Output is correct
31 Correct 4 ms 7380 KB Output is correct
32 Correct 4 ms 7400 KB Output is correct
33 Correct 5 ms 7380 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 ms 7384 KB Output is correct
36 Correct 6 ms 7508 KB Output is correct
37 Correct 4 ms 7508 KB Output is correct
38 Correct 5 ms 7764 KB Output is correct
39 Correct 5 ms 8152 KB Output is correct
40 Correct 6 ms 9556 KB Output is correct
41 Correct 76 ms 14396 KB Output is correct
42 Correct 49 ms 11112 KB Output is correct
43 Correct 50 ms 11176 KB Output is correct
44 Correct 62 ms 11188 KB Output is correct
45 Correct 49 ms 11184 KB Output is correct
46 Correct 120 ms 15040 KB Output is correct
47 Correct 119 ms 14924 KB Output is correct
48 Correct 111 ms 14964 KB Output is correct
49 Correct 120 ms 14944 KB Output is correct
50 Correct 40 ms 11428 KB Output is correct
51 Correct 39 ms 11348 KB Output is correct
52 Correct 39 ms 11436 KB Output is correct
53 Correct 83 ms 15404 KB Output is correct
54 Correct 90 ms 15440 KB Output is correct
55 Correct 101 ms 15412 KB Output is correct
56 Correct 7 ms 8920 KB Output is correct
57 Correct 32 ms 37784 KB Output is correct
58 Correct 122 ms 144800 KB Output is correct
59 Correct 208 ms 163628 KB Output is correct
60 Correct 99 ms 14700 KB Output is correct
61 Correct 108 ms 14556 KB Output is correct