Submission #823388

#TimeUsernameProblemLanguageResultExecution timeMemory
823388serifefedartarCat in a tree (BOI17_catinatree)C++17
51 / 100
139 ms19952 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000009
#define LOGN 20
#define MAXN 2005

vector<vector<int>> graph;
int N, D;
int dp[MAXN][MAXN];
void solve(int node) {
    dp[node][0] = 1;
    for (auto u : graph[node]) {
        solve(u);
        dp[node][0] += dp[u][D-1];
    }

    for (int dist = 1; dist <= N; dist++) {
        if (dist * 2 >= D) {
            for (auto u : graph[node])
                dp[node][dist] += dp[u][dist-1];            
        } else {
            int complement = D - dist - 1;
            vector<int> pref(N+1, 0), suff(N+2, 0);
            for (int i = 1; i <= graph[node].size(); i++)
                pref[i] = pref[i-1] + dp[graph[node][i-1]][complement];
            for (int i = graph[node].size(); i >= 1; i--)
                suff[i] = suff[i+1] + dp[graph[node][i-1]][complement];
            for (int i = 0; i < graph[node].size(); i++)
                dp[node][dist] = max(dp[node][dist], pref[i] + suff[i+2] + dp[graph[node][i]][dist-1]);
        }
    }

    for (int i = N; i >= 1; i--)
        dp[node][i-1] = max(dp[node][i-1], dp[node][i]);
}

int main() {
    fast
    cin >> N >> D;

    graph = vector<vector<int>>(N, vector<int>());
    for (int i = 1; i < N; i++) {
        int parent; cin >> parent;
        graph[parent].push_back(i);
    }
    solve(0);

    cout << dp[0][0] << "\n";
}

Compilation message (stderr)

catinatree.cpp: In function 'void solve(int)':
catinatree.cpp:29:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |             for (int i = 1; i <= graph[node].size(); i++)
      |                             ~~^~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:33:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |             for (int i = 0; i < graph[node].size(); i++)
      |                             ~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...