답안 #101410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101410 2019-03-19T01:14:01 Z dwsc Chase (CEOI17_chase) C++14
0 / 100
1867 ms 54056 KB
#include <bits/stdc++.h>
using namespace std;
int n,v;
int p[100010];
vector<int> adj[100010];
int memo[100010][110];
long long dp(int x,int dist,int pa){
    if (dist == -1) return -1e18;
    if (memo[x][dist] != -1) return memo[x][dist];
   // cout << x << " " << dist << " " << pa << '\n';
    long long ans = 0;
    long long sum = 0;
    for (int i = 0; i < (int)(adj[x].size()); i++){
        int nx = adj[x][i];
        if (nx == pa) continue;
        sum += p[nx];
    }
   // cout << sum << "\n";
    for (int i = 0; i < (int)(adj[x].size()); i++){
        int nx = adj[x][i];
        if (nx == pa) continue;
        ans = max(ans,dp(nx,dist-1,x)+sum);
        ans = max(ans,dp(nx,dist,x));
    }
    return memo[x][dist] = ans;
}
int main(){
    cin >> n >> v;
    for (int i = 0; i < n; i++){
        cin >> p[i];
    }
    for (int i = 0; i < n-1; i++){
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    if (n <= 1000){
        long long ans = 0;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                for (int k = 0; k <= v; k++) memo[j][k] = -1;
            }
          //  cout << dp(i,v,-1) << "\n";
            ans = max(ans,dp(i,v,-1));
        }
        cout << ans;
    }
    else{
        for (int j = 0; j < n; j++){
            for (int k = 0; k <= v; k++) memo[j][k] = -1;
        }
        cout << dp(0,v,-1);
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Incorrect 4 ms 2688 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Incorrect 4 ms 2688 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1867 ms 54056 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Incorrect 4 ms 2688 KB Output isn't correct
4 Halted 0 ms 0 KB -