답안 #483070

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483070 2021-10-27T15:43:22 Z ntabc05101 Chase (CEOI17_chase) C++17
20 / 100
272 ms 217100 KB
#include<bits/stdc++.h>
using namespace std;

const int mxN = 100005;
const int mxV = 105;

int n, v;
int P[mxN];
long long dp[mxN][mxV][2];
vector<int> adj[mxN];
long long sum[mxN];
long long res = 0;

void dfs(int u, int p = 0) {
  for (auto &to: adj[u]) {
    if (to != p) {
      dfs(to, u);
      for (int i = 1; i <= v; i++) {
        dp[u][i][1] = max(dp[u][i][1], max(dp[to][i][0], dp[to][i - 1][0] + sum[to] - P[u]));
        if (dp[u][i][1] > dp[u][i][0]) {
          swap(dp[u][i][1], dp[u][i][0]);
        }
      }
    }
  }
}

void dfs2(int u, int p = 0) {
  for (int i = 1; i <= v; i++) {
    res = max(res, max(dp[u][i][0], dp[u][i - 1][0] + sum[u]));
  }
  for (auto &to: adj[u]) {
    if (to != p) {
      long long tmp[v + 1];
      for (int i = 1; i <= v; i++) {
        tmp[i] = dp[u][i][0];
        if (max(dp[to][i][0], dp[to][i - 1][0] + sum[to] - P[u]) == dp[u][i][0]) {
          dp[u][i][0] = dp[u][i][1];
        }
        dp[to][i][1] = max(dp[to][i][1], max(dp[u][i][0], dp[u][i - 1][0] + sum[u] - P[to]));
        if (dp[to][i][1] > dp[to][i][0]) {
          swap(dp[to][i][1], dp[to][i][0]);
        }
      }
      dfs2(to, u);
      for (int i = 1; i <= v; i++) {
        dp[u][i][0] = tmp[i];
      }
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);

  cin >> n >> v;
  for (int i = 1; i <= n; i++) {
    cin >> P[i];
  }
  for (int i = 0, x, y; i < n - 1; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  for (int i = 1; i <= n; i++) {
    for (auto &to: adj[i]) {
      sum[i] += P[to];
    }
  }
  dfs(1); dfs2(1);

  cout << res << "\n";

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 4 ms 4812 KB Output is correct
8 Incorrect 3 ms 4428 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 272 ms 217100 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 4 ms 4812 KB Output is correct
8 Incorrect 3 ms 4428 KB Output isn't correct
9 Halted 0 ms 0 KB -