답안 #874139

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
874139 2023-11-16T10:30:50 Z josanneo22 Chase (CEOI17_chase) C++17
20 / 100
4000 ms 105252 KB
/*
Problem: P4657 [CEOI2017] Chase
When:    2023-11-16 14:54:19
Author:  Ning07
*/

#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)

const int N=2e5+600;
int n,V,a[N],head[N<<1],ver[N<<1],nxt[N<<1],tot=0,tmp[N];
i64 ans=0,dp[N][120][2],sum[N];
void add_edge(int x,int y){
    ver[++tot]=y;
    nxt[tot]=head[x];
    head[x]=tot;
}
void update(int nw,int son,int fa){
    L(i,1,V-1) ans=max(ans,dp[nw][i][0]+dp[son][V-i][1]);
    L(i,1,V){
        dp[nw][i][0]=max({dp[nw][i][0],dp[son][i][0],dp[son][i-1][0]+sum[nw]-a[son]});
        dp[nw][i][1]=max({dp[nw][i][1],dp[son][i][1],dp[son][i-1][1]+sum[nw]-a[fa]});
    }
}
void dfs(int u,int f){
    for(int i=head[u];i;i=nxt[i]){
        int v=ver[i];
        if(v==f) continue;
        dfs(v,u);
    }
    int cnt=0;
    L(i,1,n) dp[u][i][0]=sum[u];
    for(int i=head[u];i;i=nxt[i]){
        int v=ver[i];
        update(u,v,f);
        tmp[++cnt]=v;
    }
    L(i,1,n) dp[u][i][0]=sum[u],dp[u][i][1]=0;
    R(i,cnt,1) update(u,tmp[i],f);
    ans=max({ans,dp[u][V][0],dp[u][V][1]});
    return;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    std::cin>>n>>V;
    L(i,1,n) std::cin>>a[i];
    L(i,1,n-1){
        int u,v; 
        std::cin>>u>>v;
        add_edge(u,v);
        add_edge(v,u);
    }
    L(i,1,n){
        for(int j=head[i];j;j=nxt[j]){
            int v=ver[j];
            sum[i]+=a[v];
        }
    }
    dfs(1,0);
    cout<<ans<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4560 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4556 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4560 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4556 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Incorrect 4 ms 5212 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4026 ms 105252 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4560 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4556 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Incorrect 4 ms 5212 KB Output isn't correct
8 Halted 0 ms 0 KB -