当前位置:网站首页>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 .
边栏推荐
- Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
- nacos简易实现负载均衡
- Redis source code learning (29), compressed list learning, ziplist C (II)
- jeecg 重启报40001
- 2.2 【pytorch】torchvision.transforms
- nacos簡易實現負載均衡
- Common interview questions for embedded engineers 2-mcu_ STM32
- 【pytorch】2.4 卷积函数 nn.conv2d
- Mysql8.0 learning record 17 -create table
- 2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
猜你喜欢
[pytorch] softmax function
An overview of the design of royalties and service fees of mainstream NFT market platforms
[interview brush 101] linked list
2.2 【pytorch】torchvision.transforms
Daily practice of C language - day 80: currency change
Principles of Microcomputer - internal and external structure of microprocessor
[pytorch] 2.4 convolution function nn conv2d
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
Installation and use of NoSQL database
钓鱼识别app
随机推荐
【pytorch】nn.AdaptiveMaxPool2d
序列化、监听、自定义注解
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Graduation season, I want to tell you
Niuke monthly race 22- collect pieces of paper
How to solve the problem of fixed assets management and inventory?
Installing Oracle EE
Shell script - special variables: shell $, $*, [email protected], $$$
Input标签的type设置为number,去掉上下箭头
Shell script -while loop explanation
Mysql 优化
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
树结构---二叉树1
LogBack
Record a redis timeout
FAQ | FAQ for building applications for large screen devices
SDN_简单总结
Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization