洛谷-P8294.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <algorithm>
  2. #include <cstdint>
  3. #include <iostream>
  4. #include <limits>
  5. #include <utility>
  6. #include <vector>
  7. using int64 = std::int64_t;
  8. namespace {
  9. constexpr int64 INF = std::numeric_limits<int64>::max() / 4;
  10. int n;
  11. std::vector<int64> weight;
  12. std::vector<int> depth;
  13. std::vector<std::vector<int>> children;
  14. std::vector<std::vector<int>> subtree;
  15. std::vector<std::vector<std::pair<int, int>>> states;
  16. std::vector<int64> dp;
  17. std::vector<int64> minimum_one;
  18. std::vector<int64> minimum_two;
  19. int64& value(int first, int second) {
  20. return dp[static_cast<std::size_t>(first) * (n + 1) + second];
  21. }
  22. void relax(int64& target, int64 candidate) {
  23. if (candidate < target) target = candidate;
  24. }
  25. void prepare(int vertex) {
  26. subtree[vertex].push_back(vertex);
  27. for (int child : children[vertex]) {
  28. depth[child] = depth[vertex] + 1;
  29. prepare(child);
  30. subtree[vertex].insert(subtree[vertex].end(), subtree[child].begin(),
  31. subtree[child].end());
  32. }
  33. for (int other : subtree[vertex]) {
  34. states[vertex].push_back({vertex, other});
  35. if (other != vertex) states[vertex].push_back({other, vertex});
  36. }
  37. if (children[vertex].size() == 2) {
  38. int left = children[vertex][0];
  39. int right = children[vertex][1];
  40. for (int u : subtree[left]) {
  41. for (int v : subtree[right]) {
  42. states[vertex].push_back({u, v});
  43. states[vertex].push_back({v, u});
  44. }
  45. }
  46. }
  47. }
  48. void solve_subtree(int vertex) {
  49. if (children[vertex].empty()) {
  50. value(vertex, vertex) = weight[vertex];
  51. return;
  52. }
  53. for (int child : children[vertex]) solve_subtree(child);
  54. if (children[vertex].size() == 1) {
  55. int child = children[vertex][0];
  56. for (auto [v, i] : states[child]) {
  57. relax(value(vertex, v), value(i, v) + weight[vertex]);
  58. int u = v;
  59. relax(value(u, vertex), value(u, i) + weight[vertex] *
  60. (depth[i] - depth[vertex]) + weight[u]);
  61. }
  62. return;
  63. }
  64. int left = children[vertex][0];
  65. int right = children[vertex][1];
  66. for (int x : subtree[vertex]) minimum_one[x] = minimum_two[x] = INF;
  67. // The outgoing original weight is in one child, and the incoming weight
  68. // finishes in the other child.
  69. for (auto [j, k] : states[left]) relax(minimum_one[k], value(j, k));
  70. for (auto [j, k] : states[right]) relax(minimum_one[k], value(j, k));
  71. for (auto [i, k] : states[vertex]) {
  72. if (i != vertex && k != vertex && minimum_one[k] < INF)
  73. relax(minimum_two[i], weight[i] * (depth[k] - depth[vertex]) +
  74. minimum_one[k]);
  75. }
  76. for (auto [v, i] : states[left])
  77. relax(value(vertex, v), weight[vertex] + value(i, v) + minimum_two[i]);
  78. for (auto [v, i] : states[right])
  79. relax(value(vertex, v), weight[vertex] + value(i, v) + minimum_two[i]);
  80. for (int x : subtree[vertex]) minimum_one[x] = minimum_two[x] = INF;
  81. for (auto [j, k] : states[left])
  82. relax(minimum_one[j], value(j, k) + weight[vertex] *
  83. (depth[k] - depth[vertex]));
  84. for (auto [j, k] : states[right])
  85. relax(minimum_one[j], value(j, k) + weight[vertex] *
  86. (depth[k] - depth[vertex]));
  87. for (auto [i, j] : states[vertex]) {
  88. if (i != vertex && j != vertex && minimum_one[j] < INF)
  89. relax(minimum_two[i], weight[j] * (depth[i] - depth[vertex]) +
  90. minimum_one[j]);
  91. }
  92. for (auto [u, i] : states[left])
  93. relax(value(u, vertex), weight[u] + value(u, i) + minimum_two[i]);
  94. for (auto [u, i] : states[right])
  95. relax(value(u, vertex), weight[u] + value(u, i) + minimum_two[i]);
  96. for (int x : subtree[vertex]) minimum_one[x] = minimum_two[x] = INF;
  97. for (auto [u, i] : states[left]) {
  98. relax(minimum_one[u], value(u, i) + weight[vertex] *
  99. (depth[i] - depth[vertex]) + weight[u]);
  100. relax(minimum_two[i], value(u, i));
  101. }
  102. for (auto [u, i] : states[right]) {
  103. relax(minimum_one[u], value(u, i) + weight[vertex] *
  104. (depth[i] - depth[vertex]) + weight[u]);
  105. relax(minimum_two[i], value(u, i));
  106. }
  107. for (auto [u, v] : states[vertex]) {
  108. if (u != vertex && v != vertex)
  109. relax(value(u, v), minimum_one[u] + minimum_two[v]);
  110. }
  111. }
  112. } // namespace
  113. int main() {
  114. std::ios::sync_with_stdio(false);
  115. std::cin.tie(nullptr);
  116. std::cin >> n;
  117. weight.assign(n + 1, 0);
  118. depth.assign(n + 1, 0);
  119. children.assign(n + 1, {});
  120. subtree.assign(n + 1, {});
  121. states.assign(n + 1, {});
  122. minimum_one.assign(n + 1, INF);
  123. minimum_two.assign(n + 1, INF);
  124. dp.assign(static_cast<std::size_t>(n + 1) * (n + 1), INF);
  125. for (int i = 1; i <= n; ++i) std::cin >> weight[i];
  126. for (int i = 2; i <= n; ++i) {
  127. int parent;
  128. std::cin >> parent;
  129. children[parent].push_back(i);
  130. }
  131. if (n == 1) {
  132. std::cout << 0 << '\n';
  133. return 0;
  134. }
  135. prepare(1);
  136. for (int child : children[1]) solve_subtree(child);
  137. int64 answer = INF;
  138. if (children[1].size() == 1) {
  139. int child = children[1][0];
  140. for (auto [u, v] : states[child])
  141. relax(answer, value(u, v) + weight[1] * (depth[v] - depth[1]));
  142. } else {
  143. int first_child = children[1][0];
  144. int second_child = children[1][1];
  145. for (int direction = 0; direction < 2; ++direction) {
  146. std::fill(minimum_one.begin(), minimum_one.end(), INF);
  147. std::fill(minimum_two.begin(), minimum_two.end(), INF);
  148. for (auto [i, j] : states[first_child])
  149. relax(minimum_one[i], value(i, j) + weight[1] *
  150. (depth[j] - depth[1]));
  151. for (auto [k, l] : states[second_child])
  152. relax(minimum_two[l], value(k, l));
  153. for (auto [i, l] : states[1]) {
  154. if (i != 1 && l != 1)
  155. relax(answer, minimum_one[i] + minimum_two[l] +
  156. weight[i] * (depth[l] - depth[1]));
  157. }
  158. std::swap(first_child, second_child);
  159. }
  160. }
  161. std::cout << answer << '\n';
  162. return 0;
  163. }