别样的碰碰车大战题解
别样的碰碰车大战
太典了
不是我说,这几乎就是树型DP板子题吧
20pts
枚举每一个点选/不选,再统计答案
时间复杂度
100pts
考虑树型DP
设
很明显:如果不选
如果选
其中
Code
#include<bits/stdc++.h>
namespace Kards{
using namespace std;
typedef long long ll;
ll n,f[100005][2];
vector<pair<ll,ll> >a[100005];
void dfs(ll x,ll fa){
for(auto [v,w]:a[x]){
if(v==fa)continue;
dfs(v,x);
f[x][0]+=max(f[v][0],max(f[v][1],0ll));
f[x][1]+=max(f[v][0],max(f[v][1]+w,0ll));
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1,u,v,w;i<n;i++){
cin>>u>>v>>w;
a[u].emplace_back(v,w);
a[v].emplace_back(u,w);
}
dfs(1,0);
cout<<max(f[1][0],f[1][1]);
return 0;
}
}
int main(){return Kards::main();}
注意:
- 开
long long - 由于可以不选,因此DP时要与
0取最大值