1 solutions

  • 0
    @ 2026-7-29 10:52:26
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int n,K;
    vector<pii>v[200005];
    int f[200005][4];
    int dp[2][4000][2];
    void dfs(int x,int pre,int l){
    	vector<int>t;
    	for(auto i:v[x])if(i.first!=pre)dfs(i.first,x,i.second),t.pb(i.first);
    	if(!t.size()){
    		f[x][0]=0;
    		f[x][1]=l;
    		f[x][2]=-1e18;
    		if(K==4)f[x][3]=-1e18;
    		return;
    	}
    	int C=sqrt(t.size()/2)+100;
    	C=min(C,(int)(t.size()+1)/2);
    	f[x][0]=0;
    	REP(i,1,K)f[x][i]=-1e18;
    	REP(T,0,2){
    		shuffle(all(t),sd);
    		REP(o,0,2)REP(i,0,C*2+1)REP(j,0,2)dp[o][i][j]=-1e18;
    		dp[1][C][0]=0;
    		REP(i,0,t.size()){
    			int l=max(0ll,C-i),r=min(2*C,C+i),y=t[i];
    			REP(j,l,r+1)REP(p,0,2)dp[0][j][p]=dp[1][j][p],dp[1][j][p]=-1e18;
    			REP(j,l,r+1){
    				REP(op,0,K-2)if(dp[0][j][op]!=-1e18){
    					dp[1][j][op]=max(dp[1][j][op],dp[0][j][op]+f[y][0]);
    					dp[1][j+1][op]=max(dp[1][j+1][op],dp[0][j][op]+f[y][1]);
    					if(K==3){
    						dp[1][j-1][op]=max(dp[1][j-1][op],dp[0][j][op]+f[y][2]);
    					}else{
    						dp[1][j][op^1]=max(dp[1][j][op^1],dp[0][j][op]+f[y][2]);
    						dp[1][j-1][op]=max(dp[1][j-1][op],dp[0][j][op]+f[y][3]);
    					}
    				}
    			}
    		}
    //		if(x==3)cout<<dp[1][C+1][0]<<endl;
    		f[x][0]=max({f[x][0],dp[1][C][0],dp[1][C-1][0]+l});
    		f[x][1]=max({f[x][1],dp[1][C][0]+l});
    		f[x][2]=max({f[x][2],dp[1][C+1][0]+l});
    		if(K==4){
    			f[x][3]=max({f[x][3],dp[1][C][1]+l});
    		}
    	}
    }
    void Main() {
    	cin>>n>>K;
    	REP(i,0,n)v[i].clear();
    	REP(i,1,n){
    		int x,y,z;
    		cin>>x>>y>>z;
    		--x,--y;
    		v[x].pb({y,z});v[y].pb({x,z});
    	}
    	dfs(0,-1,-1e18);
    //	REP(i,0,4){
    //		REP(j,0,K)cout<<f[i][j]<<' ';
    //		cout<<endl;
    //	}
    	cout<<f[0][0]<<endl;
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    //	cerr<<clock()<<endl;
    }
    signed main() {
    	freopen("pretz.in","r",stdin);
    	freopen("pretz.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    3068
    Time
    2000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By