/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 5ms 5.02 MiB
#3 Accepted 5ms 5.152 MiB
#4 Accepted 5ms 5.02 MiB
#5 Accepted 5ms 5.02 MiB
#6 Accepted 7ms 5.02 MiB
#7 Accepted 9ms 5.0 MiB
#8 Accepted 9ms 5.02 MiB
#9 Accepted 11ms 5.094 MiB
#10 Accepted 12ms 5.207 MiB
#11 Accepted 12ms 5.02 MiB
#12 Accepted 362ms 393.199 MiB
#13 Accepted 376ms 393.062 MiB
#14 Accepted 366ms 393.074 MiB
#15 Accepted 365ms 393.074 MiB
#16 Accepted 347ms 393.0 MiB
#17 Accepted 355ms 393.02 MiB
#18 Accepted 357ms 393.219 MiB
#19 Accepted 372ms 393.246 MiB
#20 Accepted 348ms 393.02 MiB
#21 Accepted 381ms 393.246 MiB
#22 Accepted 336ms 393.117 MiB
#23 Accepted 373ms 393.098 MiB
#24 Accepted 358ms 393.02 MiB
#25 Accepted 354ms 393.133 MiB
#26 Accepted 332ms 393.074 MiB
#27 Accepted 350ms 393.055 MiB
#28 Accepted 324ms 393.008 MiB
#29 Accepted 378ms 393.059 MiB
#30 Accepted 355ms 393.215 MiB
#31 Accepted 374ms 393.02 MiB
#32 Accepted 377ms 393.172 MiB
#33 Accepted 376ms 393.191 MiB
#34 Accepted 339ms 393.168 MiB
#35 Accepted 343ms 393.07 MiB
#36 Accepted 345ms 393.027 MiB
#37 Accepted 379ms 393.234 MiB
#38 Accepted 345ms 393.035 MiB
#39 Accepted 366ms 393.219 MiB
#40 Accepted 348ms 393.172 MiB
#41 Accepted 375ms 393.082 MiB
#42 Accepted 326ms 392.855 MiB
#43 Accepted 330ms 393.066 MiB
#44 Accepted 311ms 392.434 MiB
#45 Accepted 306ms 392.285 MiB
#46 Accepted 310ms 392.27 MiB
#47 Accepted 346ms 405.27 MiB
#48 Accepted 340ms 399.832 MiB
#49 Accepted 385ms 398.332 MiB
#50 Accepted 337ms 398.281 MiB

Code

/**
Author : Kamonasish Roy (Bullet)
Basic DP on Tree But Medium Level, Because we need to optimize.
First we need to calculate all depth level node.
then we caluculate all its child
if it has two child transition:
DP[parent][lenght]=max(DP[child1][lenght-1],DP[child2][lenght-1])+his own weight
similary , if it has one child, we should consider it.
This way we can build a dp table.
after that we can merge if it has two child,
like Dp[parent][k]=MAX(Dp[child1][i],DP[child2][k-i])...i=2 to K-1.
Why K-1, beacause Parent itselt in the path.
But If graph is a tree, then we may have more two children.
Then we can use prefix and suffix sum...then we marge.
We can count it on the vector max prefix and suffix sum all possible his 
children.
Basically re-rooting dp technique, we can use here to merge the path sum.

**/
#include<bits/stdc++.h>
using namespace std;
const long long M=1e5+10,MOD=1e9+7;
typedef long long ll;
#define debug(x) cout<<x<<endl
ll dp[M][501];
vector<int>edge[M];
int a[M];
vector<int>par[M];
void dfs(int x,int p,int n,int k){
	dp[x][1]=a[x];
	for(auto u:edge[x]){
		if(u==p)continue;
		dfs(u,x,n,k);
		par[x].push_back(u);
		for(int i=2;i<=k;i++){
			dp[x][i]=max(dp[x][i],dp[u][i-1]+dp[x][1]);
		}
	}
	
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
   // cin>>t;
    while(t--){
    	int n,k;
    	cin>>n>>k;
    	for(int i=1;i<=n;i++)cin>>a[i];
    	for(int i=1;i<n;i++){
    		int u,v;
    		cin>>u>>v;
    		edge[u].push_back(v);
    		edge[v].push_back(u);
    	}
    	for(int i=1;i<=n;i++){
    		for(int j=0;j<=k;j++)dp[i][j]=-1e12;
    	}
    	dfs(1,-1,n,k);
    	ll mx=0;
    	for(int i=1;i<=n;i++)mx=max(mx,dp[i][k]);
    	for(int i=1;i<=n && k>2;i++){
    		if((int)par[i].size()<=1)continue;
    		vector<ll>First_best(k+1,-1e12);
    		vector<ll>Second_best(k+1,-1e12);
    		for(auto it:par[i]){
    			for(int start=1;start<=k;start++){
    				if(First_best[start]<=dp[it][start]){
    					Second_best[start]=First_best[start];
    					First_best[start]=dp[it][start];
    				}
    				else{
    					Second_best[start]=max(Second_best[start],dp[it][start]);
    				}
    			}
    		}
    		for(auto it:par[i]){
    			int l=1,r=k-2;
    			while(r>0){
    				ll second=First_best[r];
    				if(dp[it][r]==First_best[r])second=Second_best[r];
    				mx=max(mx,dp[it][l]+second+dp[i][1]);
    				l++;
    				r--;
    			}
    		}
    	}
    	cout<<mx<<"\n";
    	
    	
    	
    	
    	}
    	
    	
    	
    	
      
    return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1161 Max path sum (Hard Version)
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-12 13:03:22
Judged At
2025-02-12 13:03:22
Judged By
Score
100
Total Time
385ms
Peak Memory
405.27 MiB