链接:https://ac.nowcoder.com/acm/contest/140/H
来源:牛客网

题目描述

White Cloud has a tree with n nodes.The root is a node with number 1. Each node has a value.
White Rabbit wants to travel in the tree 3 times. In Each travel it will go through a path in the tree.
White Rabbit can't pass a node more than one time during the 3 travels. It wants to know the maximum sum value of all nodes it passes through.

输入描述:

The first line of input contains an integer n(3 <= n <= 400001)
In the next line there are n integers in range [0,1000000] denoting the value of each node.
For the next n-1 lines, each line contains two integers denoting the edge of this tree.

输出描述:

Print one integer denoting the answer.

输入

13
10 10 10 10 10 1 10 10 10 1 10 10 10
1 2
2 3
3 4
4 5
2 6
6 7
7 8
7 9
6 10
10 11
11 12
11 13

输出

110

题意:

给你一棵n个节点的树,每个点都有一个权值,选出三条不相交路径使得权值和最大

思路:

dp[i][j][0]表示以i为根的子树选了j条路径的最大权值和

dp[i][j][1]表示以i为根的子树选了j条路径,其中一条路径的一端为i的最大权值和

那么dp时来一波类似于背包的转移就好了

//https://ac.nowcoder.com/acm/contest/140/H
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
vector<int> G[500005];
int val[500005];
LL dp[500005][5][2];
void Sech(int u, int fa)
{int i, j, p, q, v, t;LL now[5][4], c[5][4], sum[5][4];memset(now, 0, sizeof(now));for(t=0;t<G[u].size();t++){v = G[u][t];if(v==fa)continue;Sech(v, u);memset(c, 0, sizeof(c));for(i=0;i<=3;i++)c[i][0] = dp[v][i][0], c[i][1] = dp[v][i][1];memset(sum, 0, sizeof(sum));for(i=0;i<=3;i++){for(j=0;i+j<=3;j++){for(p=0;p<=2;p++){for(q=0;p+q<=2;q++)sum[i+j][p+q] = max(sum[i+j][p+q], now[i][p]+c[j][q]);}}}memcpy(now, sum, sizeof(now));}for(i=0;i<=3;i++)dp[u][i][0] = now[i][0];for(i=0;i<=2;i++){for(j=0;j<=2;j++)dp[u][i+1][0] = max(dp[u][i+1][0], now[i][j]+val[u]);}for(i=0;i<=3;i++){for(j=0;j<=1;j++)dp[u][i][1] = max(dp[u][i][1], now[i][j]+val[u]);}
}
int main(void)
{int n, i, x, y;scanf("%d", &n);for(i=1;i<=n;i++)scanf("%d", &val[i]);for(i=1;i<=n-1;i++){scanf("%d%d", &x, &y);G[x].push_back(y);G[y].push_back(x);}Sech(1, 0);printf("%lld\n", dp[1][3][0]);return 0;
}

牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)相关推荐

  1. 牛客网暑期ACM多校训练营(第二场):J. farm(暴力)

    链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 题目描述 White Rabbit has a rectangular farmland of ...

  2. 2018牛客网暑期ACM多校训练营第二场 D - money(贪心)

    题目链接 https://www.nowcoder.com/acm/contest/140#question [题目描述] White Cloud is exercising in the playg ...

  3. 牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph

    牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph 做法:\(G'\) 中的对应原图两条边(a,b) (c,d)的最短路为: \[ w[a][b] + w[c][d] ...

  4. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  5. 牛客网暑期ACM多校训练营(第五场)

    牛客网暑期ACM多校训练营(第五场) A. gpa 二分答案,然后就转化为是否满足 \(\frac {\sum s[i]c[i]}{\sum s[i]} ≥ D\), \(\sum s[i]c[i] ...

  6. 牛客网暑期ACM多校训练营(第三场)

    牛客网暑期ACM多校训练营(第三场) A. PACM Team 01背包,输出方案,用bool存每种状态下用的哪一个物品,卡内存.官方题解上,说用char或者short就行了.还有一种做法是把用的物品 ...

  7. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

  8. 牛客网暑期ACM多校训练营(第十场)D Rikka with Prefix Sum

    链接:https://www.nowcoder.com/acm/contest/148/D 来源:牛客网 题目描述 Prefix Sum is a useful trick in data struc ...

  9. 牛客网暑期ACM多校训练营(第三场)A.PACM Team(多重01背包)

    链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 题目描述 Eddy was a contestant participating in ACM ...

  10. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

最新文章

  1. python argparse_Python 命令行之旅:初探 argparse
  2. java代码连接数据库
  3. 处理被中断的系统调用
  4. 天津大学计算机学院院长及副院长,李晓红 教授
  5. html 自定义打印模板,HTML+CSS入门 自定义模板详解
  6. 修改FTP服务器端口后无法访问
  7. 单自由度阻尼强迫振动通解求导及simulink验证(修正了网上常见的错误)
  8. Linux下10 个最酷的 Linux 单行命令(转载)
  9. Pwn2Own 2021奥斯汀黑客大赛公布类别、目标及奖金
  10. 如何在Azure中配置SQL Server 2008 R2故障转移群集实例
  11. emouse思·睿—评论与观点整理之一
  12. 并联机构工作空间求解_结构计算软件—结构力学求解器
  13. ad中按钮开关的符号_收藏:电路图形符号大全!!!
  14. myeclipse设置黑色主题
  15. 基于张量分解的药物重定位预测药物、靶点和疾病之间的关联
  16. web前端响应式设计总结
  17. dna数据u盘_DNA数据库黑客时代来临
  18. ubuntu18.04未发现wifi适配器,安装wifi无线网卡驱动-RTL8822BE、RTL8822CE、RTL8821CE、RTL8723DE
  19. 基于iLog3的实时日志实现
  20. QT串口助手(串口的查找和打开)

热门文章

  1. python从入门到放弃表情图-[python从入门到放弃]基于百度OCR的文字识别
  2. 基于ARM的非特定人语音识别系统设计
  3. 微信计步器怎么不计步_送我一顶圣诞帽@星尘StarDust,制作一个圣诞创意微信头像...
  4. Vue的babel-plugin-transform-remove-console依赖使用方法
  5. php将mysql转换为json字符串_在PHP中将MySQL记录集转换为JSON字符串
  6. flock用法详解 linux_netstat命令详解
  7. Vue 强制刷新组件
  8. 【java笔记】线程(5):线程安全问题
  9. 【数据结构和算法笔记】数组(数组的储存方式和特殊矩阵的压缩储存)
  10. 可控硅型号怎样识别_可控硅是什么_可控硅型号_可控硅分类及判别_可控硅种类...