Submission #355438

# Submission time Handle Problem Language Result Execution time Memory
355438 2021-01-22T13:25:30 Z spatarel Cat in a tree (BOI17_catinatree) C++17
0 / 100
109 ms 139628 KB
#include <stdio.h>
#include <algorithm>
#include <deque>
#include <vector>

const int MAX_N = 200000;

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

std::deque<int> maxN[MAX_N];
int depth[MAX_N];

void dfs(int u) {
  depth[u] = 1;
  int deepestChild = -1;
  int secondDepth = 0;
  for (int v : children[u]) {
    dfs(v);
    if (depth[u] <= 1 + depth[v]) {
      secondDepth = depth[u];
      depth[u] = 1 + depth[v];
      deepestChild = v;
    } else if (secondDepth < 1 + depth[v]) {
      secondDepth = 1 + depth[v];
    }
  }
  if (secondDepth >= D / 2) {
    maxN[u] = std::deque<int>(1 + D);
    maxN[u][0] = 1;
    for (int v : children[u]) {
      if (depth[v] > D - 1) {
        maxN[u][0] += maxN[v][D - 1];
      }
    }
    std::vector<int> maxAdd(1 + D);
    for (int v : children[u]) {
      for (int d = 0; d < D && d < depth[v]; d++) {
        maxN[u][d + 1] += maxN[v][d];
      }
      for (int d = 0; d < D - d - 2 && d < depth[v]; d++) {
        maxAdd[d + 1] = std::max(maxAdd[d + 1],
            maxN[v][d] - (D - d - 2 < depth[v] ? maxN[v][D - d - 2] : 0));
      }
    }
    for (int d = 0; d < D - d - 2; d++) {
      maxN[u][d + 1] = maxAdd[d + 1] + maxN[u][D - d - 1];
    }
    for (int d = D - 1; d >= 1; d--) {
      maxN[u][d - 1] = std::max(maxN[u][d - 1], maxN[u][d]);
    }
  } else {
    //printf("u = %d\n", u);
    //fflush(stdout);
    std::vector<int> maxAdd(secondDepth);
    //printf("second depth = %d\n", secondDepth);
    //fflush(stdout);
    for (int v : children[u]) {
      for (int d = 0; d < D - d - 2 && d < depth[v] && d + 1 < secondDepth; d++) {
        maxAdd[d + 1] = std::max(maxAdd[d + 1],
            maxN[v][d] /* - maxN[v][D - d - 2] */);
      }
    }
    //printf("tata\n");
    //fflush(stdout);
    if (deepestChild != -1) {
      std::swap(maxN[u], maxN[deepestChild]);
    }
    //printf("mama\n");
    //fflush(stdout);
    maxN[u].push_front(0);
    maxN[u][0] = 1;
    for (int v : children[u]) {
      if (depth[v] > D - 1) {
        maxN[u][0] += maxN[v][D - 1];
      }
    }
    printf("maxN[u][0] = %d\n", maxN[u][0]);
    fflush(stdout);
    for (int v : children[u]) {
      if (v != deepestChild) {
        for (int d = 0; d < D && d < depth[v]; d++) {
          maxN[u][d + 1] += maxN[v][d];
        }
      }
    }
    for (int d = 0; d + 1 < secondDepth; d++) {
      maxN[u][d + 1] = maxAdd[d + 1] /* + maxN[u][D - d - 1] */;
    }
    for (int d = secondDepth; 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 < depth[u]; d++) {
    printf("%d, ", maxN[u][d]);
  }
  printf("}\n");
  fflush(stdout);//*/
}

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:104:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  104 |   scanf("%d%d", &N, &D);
      |   ~~~~~^~~~~~~~~~~~~~~~
catinatree.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  107 |     scanf("%d", &xi);
      |     ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 109 ms 139628 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 109 ms 139628 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 109 ms 139628 KB Output isn't correct
2 Halted 0 ms 0 KB -