Submission #520196

# Submission time Handle Problem Language Result Execution time Memory
520196 2022-01-28T19:02:39 Z Yazan_Alattar Cat in a tree (BOI17_catinatree) C++14
0 / 100
0 ms 332 KB
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 2007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

vector <int> adj[M];
ll n, d, dp[M][M], sum[M], ans;

void dfs(int node)
{
    for(auto i : adj[node]) dfs(i);
    for(int i = 0; i <= d; ++i) sum[i] = 0;
    for(auto i : adj[node]){
        for(int j = 1; j <= d; ++j){
            sum[j] += dp[i][j - 1];
            dp[node][j] = max(dp[node][j], dp[i][j - 1]);
        }
    }
    for(int j = 1; j < d; ++j){
        if(2 * d - 2 * j < d) break;
        for(auto i : adj[node]){
            dp[node][j] = max(dp[node][j], sum[d - j] - dp[i][d - j - 1] + dp[i][j - 1]);
        }
    }
    dp[node][0] = 1 + sum[d];
    ans = max(ans, dp[node][0]);
    for(int i = d; i >= 0; --i) dp[node][i] = max(dp[node][i], dp[node][i + 1]);
    return;
}

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> d;
    for(int i = 2; i <= n; ++i){
        int x;
        cin >> x;
        adj[x + 1].pb(i);
    }
    dfs(1);
    cout << ans << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -