当前位置:网站首页>第6届蓝桥杯
第6届蓝桥杯
2022-06-27 07:35:00 【#math.h】
第10题 生命之树(树形dp)
给定一颗无根树,求出一个字数,所有节点的权值之和最大。求出最大的数是多少。
题目思路:
对于每个结点的决策有2种,分别是选择和不选择,那么我们定义dp[ i ][ 0 ] 和 dp[ i ][ 1 ]分别表示不选择(选择) i 结点能得到的最大权值和。
状态转移方程是:dp [ i ] [ 1 ] = sum(max(dp[ j ][ 1 ] , dp[ j ][ 0 ])); j 是 i 的孩子结点 。
dp[ i ][ 0 ] = 0;
#include<cstdio>
#include<cstring>
#include<vector>
#define N 100005
using namespace std;
vector<int> node[N];
// dp[i][0],dp[i][1];
// 分别表示选i结点和不选能得到的最大分数
int dp[N][2];
int v[N],vis[N];
int n,a,b;
void dfs(int u){
dp[u][1] = v[u];
dp[u][0] = 0;
vis[u]=1;
for(int i=0 ;i<node[u].size();i++){
if(!vis[node[u][i]]){
dfs(node[u][i]);
dp[u][1] += max(dp[node[u][i]][1],dp[node[u][i]][0]);
}else{
dp[u][1] = max(dp[u][1],v[u]);
dp[u][0] = max(dp[u][0],0);
}
}
}
void init(){
memset(v,0,sizeof(v));
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=1 ;i<=n ;i++){
scanf("%d",&v[i]);
}
for(int i=1 ;i<n ;i++){
scanf("%d%d",&a,&b);
node[a].push_back(b);
node[b].push_back(a);
}
}
int main(){
init();
dfs(1);
int ans = -1;
for(int i=1 ;i<=n ;i++){
// printf("dp[%d][1]:%d\n",i,dp[i][1]);
// printf("dp[%d][0]:%d\n",i,dp[i][0]);
ans = max(ans,dp[i][1]);
ans = max(ans,dp[i][0]);
}
printf("%d\n",ans);
return 0;
}
边栏推荐
猜你喜欢

磁选机是什么?

How to view program running time (timer) in JS
![log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.](/img/2c/425993cef31dd4c786f9cc5ff081ef.png)
log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.

Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé

One person manages 1000 servers? This automatic operation and maintenance tool must be mastered

js中判断成绩是否合格,范围在0-100,否则重新输入

JS uses the while cycle to calculate how many years it will take to grow from 1000 yuan to 5000 yuan if the interest rate for many years of investment is 5%

Error in idea connection database
![[compilation principles] review outline of compilation principles of Shandong University](/img/a6/b522a728ff21085411e7452f95872a.png)
[compilation principles] review outline of compilation principles of Shandong University

「短视频」临夏消防救援支队开展消防安全培训授课
随机推荐
攻防演习防御体系构建之第二篇之应对攻击的常用策略
R language consumption behavior statistics based on association rules and cluster analysis
yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
How to bind SQL statements to web buttons
(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
Implementation principle of similarity method in Oracle
What are the specialties of database system engineers?
Remote connection raspberry pie in VNC Viewer Mode
JS to judge the odd and even function and find the function of circular area
JDBC读取Mysql数据列表
js来打印1-100间的质数并求总个数优化版
在线文本数字识别列表求和工具
通过uview让tabbar根据权限显示相应数量的tabbar
JDBC parameterized query example
用XGBoost迭代读取数据集
js判断用户输入的数是否为质数(多种方法)
What is the difference between volatile and synchronized?
请问网页按钮怎么绑定sql语句呀
Gérer 1000 serveurs par personne? Cet outil d'automatisation o & M doit être maîtrisé
Coal crusher