代码:
#include <bits/stdc++.h>
#define re register
using namespace std;
typedef long long ll;
const ll N=105;
ll n ,m ,val[N],ch[N][3],dp[N][N];
//存边
struct node{
ll to,v;
};
vector<node>edge[N];
//读优
inline ll read()
{
re ll x=0;re char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return x;
}
//读入
void Input()
{
n=read();m=read();
m++;
for(re ll i=1;i<n;i++)
{
ll a=read(),b=read(),c=read();
edge[a].push_back((node){b,c});
edge[b].push_back((node){a,c});
}
}
//dfs
inline void dfs(ll x,ll f)
{
ll k=0;
for(re ll i =0;i<edge[x].size();i++)
{
ll y=edge[x][i].to;
if(y!=f)
{
dfs(y,x);
val[y]=edge[x][i].v;
ch[x][k++]=y;
}
}
}
//树形DP
inline void Dp(ll x)
{
ll ls=ch[x][0],rs=ch[x][1];
if(ls!=0 && rs!=0)
{
Dp(ls);Dp(rs);
for(re ll i=1;i<=m;i++)
for(re ll j=0;j<=i;j++)
dp[x][i]=max(dp[x][i],dp[ls][j]+dp[rs][i-j]);
}
for(re ll i=m;i>=1;i--)
dp[x][i]=dp[x][i-1]+val[x];
}
//主函数
int main()
{
Input();
dfs(1,0);
Dp(1);
if(edge[1].size()==0) puts("0");
else printf("%lld\n",dp[1][m]);
return 0;
}
洛谷:
牛客网:
您的代码已保存
答案错误:您提交的程序没有通过所有的测试用例
case通过率为70.00%
用例:
100 70 55 30 10833 91 33 27463 76 36 4658 87 34 21678 73 99 2598 24 93 12635 63 11 19643 78 73 680 1 32 4102 37 92 23500 21 10 3604 2 43 15177 89 31 29852 42 54 23887 39 94 11507 12 8 18181 43 91 12145 100 32 5967 40 20 1401 64 22 13242 35 75 18221 72 85 1334 81 65 16788 60 1 11789 35 78 29355 65 43 22186 7 64 15172 55 6 837 80 47 15163 5 79 11530 85 24 59 15 26 11049 98 10 26221 68 96 21657 27 13 26808 82 47 4806 4 52 27803 7 7 11514 27 83 23780 48 6 2596 19 22 4472 46 87 29005 42 23 20093 75 16 122 3 71 3848 3 29 20809 68 88 24818 28 39 27839 61 62 4383 84 36 19947 57 21 6160 77 93 13011 98 86 15293 58 13 23502 10 74 3756 25 44 10445 70 41 7033 60 9 20732 48 69 16033 65 76 14326 28 66 17533 50 30 8436 18 4 6828 9 51 2214 31 29 12578 47 53 3373 94 37 27045 23 12 19166 13 62 26796 54 28 8410 85 26 20656 79 18 19830 45 56 10697 67 76 11134 46 55 20804 92 83 17368 63 36 837 58 49 20706 32 99 24075 41 8 29549 96 97 22593 94 95 15797 90 45 3338 44 78 2391 67 38 5637 59 19 6804 49 23 434 66 97 24911 17 83 21330 81 91 11338 21 3 20184 40 98 11696 45 86 27052 22 14 2170 74 2 1010 20 53 16228 69 44 10951 18 5 18315 97 72 13027
对应输出应该为:
0
你的输出为:
45933