当前位置:网站首页>Niuke monthly race 22 tree sub chain
Niuke monthly race 22 tree sub chain
2022-07-01 09:13:00 【Lovely and beautiful girl】
The question :
Just give you a number , Each node has a weight , Now let me ask you what is the maximum sum of the weights of all nodes in a chain .
reflection :
In fact, this is the method of tree diameter , But this is a point right, not a side right , If it is edge weight, use it twice dfs Or on a tree dp. two dfs It can also find the two endpoints of the tree diameter , But we can't do negative side weight . This problem is on the tree dp That's it .
Code :
int T,n,m,k;
int maxn = -inf;
int va[N];
int dp[N];
vector<int > e[N];
void dfs(int now,int p)
{
dp[now] = va[now];
for(auto spot:e[now])
{
if(spot==p) continue;
dfs(spot,now);
maxn = max(maxn,dp[now]+dp[spot]); //now The chain of walking and another chain add up
dp[now] = max(dp[now],va[now]+dp[spot]); //now Which chain to follow
}
maxn = max(maxn,dp[now]); // If only one chain
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++) cin>>va[i];
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].pb(b);
e[b].pb(a);
}
dfs(1,0);
cout<<maxn;
return 0;
}
Maintain maximum and sub maximum practices ( It is the same as seeking the edge right ):
int T,n,m,k;
int maxn = -inf;
int va[N];
int dp[N][2];
vector<int > e[N];
void dfs(int now,int p)
{
dp[now][0] = dp[now][1] = va[now];
for(auto spot:e[now])
{
if(spot==p) continue;
dfs(spot,now);
if(dp[now][0]<=dp[spot][0]+va[now])
{
dp[now][1] = dp[now][0];
dp[now][0] = dp[spot][0]+va[now];
}
else if(dp[spot][0]+va[now]>dp[now][1]) dp[now][1] = dp[spot][0]+va[now];
}
maxn = max(maxn,dp[now][0]+dp[now][1]-va[now]); // The subtraction is because the next largest value also selects the current point , To get rid of .
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++) cin>>va[i];
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].pb(b);
e[b].pb(a);
}
dfs(1,0);
cout<<maxn;
return 0;
}
Find the diameter of the tree ( Border right ):
int T,n,m,k;
int maxn;
in
t va[N];
int dp[N][2];
vector<PII > e[N];
void dfs(int now,int p)
{
for(auto t:e[now])
{
int spot = t.fi,w = t.se;
if(spot==p) continue;
dfs(spot,now);
if(dp[now][0]<dp[spot][0]+w)
{
dp[now][1] = dp[now][0];
dp[now][0] = dp[spot][0]+w;
}
else if(dp[spot][0]+w>dp[now][1]) dp[now][1] = dp[spot][0]+w;
}
maxn = max(maxn,dp[now][0]+dp[now][1]);
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<n;i++)
{
int a,b,c;
cin>>a>>b>>c;
e[a].pb({
b,c});
e[b].pb({
a,c});
}
dfs(1,0);
cout<<maxn;
return 0;
}
summary :
Think more , Make up for weaknesses .
边栏推荐
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
- Nacos service configuration and persistence configuration
- Flink面试题
- Principle and application of single chip microcomputer timer, serial communication and interrupt system
- Tree structure -- binary tree 2 non recursive traversal
- 猿人学第20题(题目会不定时更新)
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- How to effectively align team cognition
- Shell脚本-变量的定义、赋值和删除
- phpexcel 里 获取某一列的列表 获取某一列的字母
猜你喜欢

OSPF - virtual link details (including configuration commands)

Mise en œuvre simple de l'équilibrage de la charge par nacos

2.2 【pytorch】torchvision. transforms

Preparing for the Blue Bridge Cup -- bit operation

I use flask to write the website "one"

Redis -- lattice connects to redis cluster

NoSQL数据库的安装和使用

Principle and application of single chip microcomputer timer, serial communication and interrupt system

Principles of Microcomputer - Introduction

Jetson Nano 安装TensorFlow GPU及问题解决
随机推荐
Shell script -read command: read data entered from the keyboard
Key points of NFT supervision and overseas policies
Databinding source code analysis
Graduation season, I want to tell you
How to realize the usage of connecting multiple databases in idel
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
[pytorch] 2.4 convolution function nn conv2d
phpexcel 里 获取某一列的列表 获取某一列的字母
Which method is good for the management of fixed assets of small and medium-sized enterprises?
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
NoSQL数据库的安装和使用
Football and basketball game score live broadcast platform source code /app development and construction project
Personal decoration notes
NiO zero copy
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
Principles of Microcomputer - internal and external structure of microprocessor
OSPF - virtual link details (including configuration commands)
Flink interview questions
3D打印Arduino 四轴飞行器
Shell script - array definition and getting array elements