Submission #1053462

#TimeUsernameProblemLanguageResultExecution timeMemory
1053462PanosPaskCat in a tree (BOI17_catinatree)C++14
0 / 100
0 ms348 KiB
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

const int INF = 1e6;

int N, D;
vector<vector<int>> adj_list;

//dp[node][dist]: Most nodes placed in the subtree of node
// if the closest node is at least dist away from node
vector<deque<int>> dp;

void makemax(int& a, const int& b)
{
    if (a < b) {
        a = b;
    }
}

// Merge the 2 dps and store the result in d1
void merge(deque<int>& v1, deque<int>& v2)
{
    if (v1.size() < v2.size()) {
        swap(v1, v2);
    }

    for (int d2 = 0; d2 < v2.size(); d2++) {
        int d1 = D - d2;
        if (d1 >= v1.size()) {
            continue;
        }

        makemax(v1[min(d2, d1)], v1[d1] + v2[d2]);
    }

    for (int dist = 0; dist < v2.size(); dist++) {
        makemax(v1[dist], v2[dist]);
    }

    // Propagate downwards
    for (int dist = v2.size() - 2; dist >= 0; dist--) {
        makemax(v1[dist], v1[dist + 1]);
    }
}

void dfs(int node, int par)
{
    dp[node].push_back(1);

    for (auto neigh : adj_list[node]) {
        if (neigh == par) {
            continue;
        }

        dfs(neigh, node);

        // Move all of dp[neigh] one step higher
        dp[neigh].push_front(-INF);

        merge(dp[node], dp[neigh]);
    }
}

int main(void)
{
    scanf("%d %d", &N, &D);

    adj_list.resize(N);
    dp.resize(N);

    for (int i = 1; i < N; i++) {
        int p;
        scanf("%d", &p);

        adj_list[p].pb(i);
    }

    dfs(0, 0);

    printf("%d\n", dp[0][0]);
}

Compilation message (stderr)

catinatree.cpp: In function 'void merge(std::deque<int>&, std::deque<int>&)':
catinatree.cpp:29:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int d2 = 0; d2 < v2.size(); d2++) {
      |                      ~~~^~~~~~~~~~~
catinatree.cpp:31:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         if (d1 >= v1.size()) {
      |             ~~~^~~~~~~~~~~~
catinatree.cpp:38:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int dist = 0; dist < v2.size(); dist++) {
      |                        ~~~~~^~~~~~~~~~~
catinatree.cpp: In function 'int main()':
catinatree.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |     scanf("%d %d", &N, &D);
      |     ~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         scanf("%d", &p);
      |         ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...