제출 #1106976

#제출 시각아이디문제언어결과실행 시간메모리
1106976LucaLucaMCat in a tree (BOI17_catinatree)C++17
0 / 100
2 ms11088 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#warning That's not the baby, that's my baby

#define debug(x) #x << " = " << x << '\n'
using ll = long long;

const int INF = 1e9;
const int NMAX = 2e5;

std::vector<int> dp[NMAX + 1];
std::vector<int> g[NMAX + 1];
int depth[NMAX + 1];
int deepest[NMAX + 1];

int n, d;

void join(std::vector<int> &a, const std::vector<int> &b) {
  for (int i = 0; i <= (int) b.size() && i <= d - 1; i++) {
    if (d - 1 - i < (int) a.size()) {
      a[d - 1 - i] = std::max(a[d - i - 1], a[d - 1 - i] + b[i]);
    }
  }
}

void dfs(int u) {
  deepest[u] = depth[u];
  int with = -1;
  for (const auto &v : g[u]) {
    depth[v] = 1 + depth[u];
    dfs(v);
    if (deepest[v] > deepest[u]) {
      deepest[u] = deepest[v];
      with = v;
    }
  }
  if (with == -1) { // frunza
    dp[u].resize(1);
    dp[u][0] = 1;
    return;
  }
  std::swap(dp[u], dp[with]);
  dp[u].insert(dp[u].begin(), 1);
  for (const auto &v : g[u]) {
    if (v != with) {
      join(dp[u], dp[v]);   
    }
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  #ifdef LOCAL
freopen("input.txt", "r", stdin);
  #endif

  std::cin >> n >> d;
  for (int i = 1; i < n; i++) {
    int p;
    std::cin >> p;
    g[p + 1].push_back(i + 1);
  }
  dfs(1);
  std::cout << *std::max_element(dp[1].begin(), dp[1].end()) << '\n';

  return 0;
}
// daca nu da accepted e vina lui valeriu

컴파일 시 표준 에러 (stderr) 메시지

catinatree.cpp:5:2: warning: #warning That's not the baby, that's my baby [-Wcpp]
    5 | #warning That's not the baby, that's my baby
      |  ^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...