Submission #354242

# Submission time Handle Problem Language Result Execution time Memory
354242 2021-01-21T15:30:58 Z spatarel Cat in a tree (BOI17_catinatree) C++17
0 / 100
1 ms 364 KB
#include <stdio.h>
#include <algorithm>
#include <vector>

const int MAX_N = 1500; // 200000

int N, D;
std::vector<int> children[MAX_N];

int maxN[MAX_N][1 + MAX_N];

void dfs(int u) {
  for (int v : children[u]) {
    dfs(v);
  }
  maxN[u][0] = 1;
  for (int v : children[u]) {
    maxN[u][0] += maxN[v][D - 1];
  }
  std::vector<int> maxAdd(D);
  for (int v : children[u]) {
    for (int d = 1; d < D; d++) {
      if (d * 2 >= D) {
        maxN[u][d] += maxN[v][d - 1];
      } else {
        maxN[u][d] += maxN[v][D - d - 1];
        maxAdd[d] = std::max(maxAdd[d], maxN[v][d - 1] - maxN[v][D - d - 1]);
      }
    }
  }
  for (int d = D - 1; d >= 1; d--) {
    maxN[u][d] += maxAdd[d];
  }
  for (int d = D - 1; d >= 1; d--) {
    maxN[u][d - 1] = std::max(maxN[u][d - 1], maxN[u][d]);
  }
  /*
  for (int d = 1; d < D; d++) {
    if (d * 2 >= D) { // d >= D / 2
      maxN[u][d] = 0;
      for (int v : children[u]) {
        maxN[u][d] += maxN[v][d - 1];
      }
    } else {
      maxN[u][d] = 0;
      int maxAdd = 0;
      for (int v : children[u]) {
        maxN[u][d] += maxN[v][D - d - 1];
        maxAdd = std::max(maxAdd, maxN[v][d - 1] - maxN[v][D - d - 1]);
      }
      maxN[u][d] += maxAdd;
    }
  }
  for (int d = D - 1; d >= 1; d--) {
    maxN[u][d - 1] = std::max(maxN[u][d - 1], maxN[u][d]);
  }//*/
  //*
  printf("maxN[%d] = {", u);
  for (int d = 0; d < D; d++) {
    printf("%d, ", maxN[u][d]);
  }
  printf("}\n");//*/
}

int main() {
  scanf("%d%d", &N, &D);
  for (int i = 1; i < N; i++) {
    int xi;
    scanf("%d", &xi);
    children[xi].push_back(i);
  }
  dfs(0);
  int answer = maxN[0][0];
  printf("%d\n", answer);
  return 0;
}

Compilation message

catinatree.cpp: In function 'int main()':
catinatree.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |   scanf("%d%d", &N, &D);
      |   ~~~~~^~~~~~~~~~~~~~~~
catinatree.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |     scanf("%d", &xi);
      |     ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -