用户工具

站点工具


2020-2021:teams:legal_string:组队训练比赛记录:contest21

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
2020-2021:teams:legal_string:组队训练比赛记录:contest21 [2021/10/02 21:49]
jxm2001
2020-2021:teams:legal_string:组队训练比赛记录:contest21 [2021/10/04 17:04] (当前版本)
jxm2001 [题解]
行 8: 行 8:
 |  F  |  0  |  0  |  0  |  |  F  |  0  |  0  |  0  | 
 |  G  |  2  |  0  |  0  |  ​ |  G  |  2  |  0  |  0  |  ​
-|  H  |  ​ ​| ​ 0  |  0  |  ​+|  H  |  ​ ​| ​ 0  |  0  |  ​
 |  K  |  0  |  0  |  0  | |  K  |  0  |  0  |  0  |
  
行 140: 行 140:
  
 二分答案 $+$ 滑动窗口,赛后一分钟过样例 $\to$ 过题,乐。 二分答案 $+$ 滑动窗口,赛后一分钟过样例 $\to$ 过题,乐。
 +
 +===== H. travel =====
 +
 +==== 题意 ====
 +
 +给定一棵点权树,从树上选三条不相交的路径,每条路径的权值定义为路径上的点权和,要求最大化三条路径权值和。
 +
 +==== 题解 ====
 +
 +设 $\text{dp}(u,​0/​1/​2,​i)$ 表示只考虑 $u$ 的子树,结点 $u$ 的状态为 $0/1/2$ 时,已经选中了 $i$ 条链此时的最大路径权值和。
 +
 +我们需要维护一条正在生成的链,这条链不包含在已经选中的 $i$ 条链当中,如果 $u$ 状态为 $0$ 表示 $u$ 不在生成链中。
 +
 +如果 $u$ 状态为 $1$ 表示 $u$ 在生成链中且 $u$ 只有一个儿子在生成链中, $u$ 状态为 $2$ 表示 $u$ 在生成链中且 $u$ 有两个儿子在生成链中。
 +
 +考虑状态转移,利用生成链的合并,不难有
 +
 +$$
 +\text{dp}(u,​0,​i+j)\gets \text{dp}(u,​0,​i)+\text{dp}(v,​0,​j)\\
 +\text{dp}(u,​1,​i+j)\gets \text{dp}(u,​0,​i)+\text{dp}(v,​1,​j)+a_u\\
 +\text{dp}(u,​1,​i+j)\gets \text{dp}(u,​1,​i)+\text{dp}(v,​0,​j)\\
 +\text{dp}(u,​2,​i+j)\gets \text{dp}(u,​1,​i)+\text{dp}(v,​1,​j)\\
 +\text{dp}(u,​2,​i+j)\gets \text{dp}(u,​2,​i)+\text{dp}(v,​0,​j)
 +$$
 +
 +注意上式的 $\gets$ 表示取最大值,另外为了防止选中复数条从 $v$ 生成的链,需要开一个临时数组存储中间量。
 +
 +初始状态为 $\text{dp}(u,​1,​0)=a_u$,最后转移完要考虑将正在生成的链转化为已经选中的链,于是有 ​
 +
 +$$
 +\text{dp}(u,​0,​i)\gets \max(\text{dp}(u,​1,​i-1),​\text{dp}(u,​2,​i-1))
 +$$
 +
 +最终答案为 $\text{dp}(1,​0,​k)$,时间复杂度 $O(nk^2)$,其中 $k$ 表示最多能选中的链的个数。本题 $k=3$。
 +
 +[[http://​tokitsukaze.live/​2018/​07/​24/​2018niuke2.H/​|参考资料]]
 +
 +<hidden 查看代码>​
 +<code cpp>
 +const int MAXN=4e5+5,​MAXK=4;​
 +struct Edge{
 + int to,next;
 +}edge[MAXN<<​1];​
 +int a[MAXN],​head[MAXN],​edge_cnt;​
 +LL dp[MAXN][3][MAXK],​tmp[3][MAXK];​
 +void Insert(int u,int v){
 + edge[++edge_cnt]=Edge{v,​head[u]};​
 + head[u]=edge_cnt;​
 +}
 +void Max(LL &a,LL b){
 + if(b>a)
 + a=b;
 +}
 +void dfs(int u,int fa){
 + dp[u][1][0]=a[u];​
 + for(int i=head[u];​i;​i=edge[i].next){
 + int v=edge[i].to;​
 + if(v==fa)continue;​
 + dfs(v,u);
 + _for(i,​0,​3)_for(j,​0,​MAXK)
 + tmp[i][j]=dp[u][i][j];​
 + _for(i,​0,​MAXK)_for(j,​0,​MAXK-i){
 + Max(tmp[0][i+j],​dp[u][0][i]+dp[v][0][j]);​
 + Max(tmp[1][i+j],​dp[u][0][i]+dp[v][1][j]+a[u]);​
 + Max(tmp[1][i+j],​dp[u][1][i]+dp[v][0][j]);​
 + Max(tmp[2][i+j],​dp[u][1][i]+dp[v][1][j]);​
 + Max(tmp[2][i+j],​dp[u][2][i]+dp[v][0][j]);​
 + }
 + _for(i,​0,​3)_for(j,​0,​MAXK)
 + dp[u][i][j]=tmp[i][j];​
 + }
 + _for(i,​1,​MAXK)
 + Max(dp[u][0][i],​max(dp[u][1][i-1],​dp[u][2][i-1]));​
 +}
 +int main()
 +{
 + int n=read_int();​
 + _rep(i,​1,​n)
 + a[i]=read_int();​
 + _for(i,​1,​n){
 + int u=read_int(),​v=read_int();​
 + Insert(u,​v);​
 + Insert(v,​u);​
 + }
 + dfs(1,0);
 + enter(dp[1][0][3]);​
 + return 0;
 +}
 +</​code>​
 +</​hidden>​
  
 ===== J. farm ==== ===== J. farm ====
2020-2021/teams/legal_string/组队训练比赛记录/contest21.1633182575.txt.gz · 最后更改: 2021/10/02 21:49 由 jxm2001