当前位置:网站首页>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 .
边栏推荐
- [video game training] real topic of 2013 video game of infrared optical communication device
- Can diffusion models be regarded as an autoencoder?
- Log4j 日志框架
- Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
- 【pytorch】nn. Crossentropyloss() and nn NLLLoss()
- Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
- Mysql 优化
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- 类加载
猜你喜欢

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据

Nacos service configuration and persistence configuration
![delete和delete[]引发的问题](/img/d9/a1c3e5ce51ef1be366a973aa42d1f0.png)
delete和delete[]引发的问题

How to manage fixed assets efficiently in one stop?

nacos服务配置和持久化配置

2.4 activation function

nacos简易实现负载均衡

2.2 【pytorch】torchvision.transforms

Design and manufacture of simple digital display electronic scale

Pain points and solutions of fixed assets management of group companies
随机推荐
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
LogBack
【pytorch】2.4 卷积函数 nn.conv2d
Imitation of Baidu search results top navigation bar effect
Principles of Microcomputer - Introduction
[video game training] real topic of 2013 video game of infrared optical communication device
JCL and slf4j
Football and basketball game score live broadcast platform source code /app development and construction project
Shell script - array definition and getting array elements
Shell script echo command escape character
Shell script -while loop explanation
JCL 和 SLF4J
Shell script - special variables: shell $, $*, [email protected], $$$
如何高效拉齐团队认知
树结构---二叉树1
美团2022年机试
Shell script - definition, assignment and deletion of variables
Principles of Microcomputer - internal and external structure of microprocessor
Mysql8.0 learning record 17 -create table
Shell script - string