답안 #483090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483090 2021-10-27T16:35:00 Z ntabc05101 Chase (CEOI17_chase) C++17
100 / 100
342 ms 218700 KB
#include<bits/stdc++.h>
using namespace std;

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

int n, v;
long long 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]);
        }*/
        long long val = max(dp[to][i][0], dp[to][i - 1][0] + sum[to] - P[u]);
        if (val > dp[u][i][0]) {
          dp[u][i][1] = dp[u][i][0]; dp[u][i][0] = val;
        }
        else {
          if (val > dp[u][i][1]) {
            dp[u][i][1] = val;
          }
        }
      }
    }
  }
}

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];
        }
      }
      for (int i = 1; i <= v; i++) {
        /*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]);
        }*/
        long long val = max(dp[u][i][0], dp[u][i - 1][0] + sum[u] - P[to]);
        if (val > dp[to][i][0]) {
          dp[to][i][1] = dp[to][i][0]; dp[to][i][0] = val;
        }
        else {
          if (val > dp[to][i][1]) {
            dp[to][i][1] = val;
          }
        }
      }
      dfs2(to, u);
      for (int i = 1; i <= v; i++) {
        dp[u][i][0] = tmp[i];
      }
    }
  }
}

int main() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }

  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;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
chase.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 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 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 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 3 ms 4812 KB Output is correct
8 Correct 2 ms 4428 KB Output is correct
9 Correct 3 ms 4300 KB Output is correct
10 Correct 3 ms 4300 KB Output is correct
11 Correct 3 ms 4300 KB Output is correct
12 Correct 2 ms 4300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 342 ms 218308 KB Output is correct
2 Correct 260 ms 218008 KB Output is correct
3 Correct 163 ms 172036 KB Output is correct
4 Correct 163 ms 171784 KB Output is correct
5 Correct 271 ms 171820 KB Output is correct
6 Correct 271 ms 171804 KB Output is correct
7 Correct 288 ms 171756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 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 3 ms 4812 KB Output is correct
8 Correct 2 ms 4428 KB Output is correct
9 Correct 3 ms 4300 KB Output is correct
10 Correct 3 ms 4300 KB Output is correct
11 Correct 3 ms 4300 KB Output is correct
12 Correct 2 ms 4300 KB Output is correct
13 Correct 342 ms 218308 KB Output is correct
14 Correct 260 ms 218008 KB Output is correct
15 Correct 163 ms 172036 KB Output is correct
16 Correct 163 ms 171784 KB Output is correct
17 Correct 271 ms 171820 KB Output is correct
18 Correct 271 ms 171804 KB Output is correct
19 Correct 288 ms 171756 KB Output is correct
20 Correct 264 ms 171796 KB Output is correct
21 Correct 44 ms 7364 KB Output is correct
22 Correct 256 ms 171780 KB Output is correct
23 Correct 172 ms 171788 KB Output is correct
24 Correct 266 ms 171844 KB Output is correct
25 Correct 165 ms 172052 KB Output is correct
26 Correct 278 ms 218700 KB Output is correct
27 Correct 282 ms 218692 KB Output is correct