这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
2020-2021:teams:legal_string:组队训练比赛记录:缓冲区 [2021/08/19 10:49] jxm2001 |
2020-2021:teams:legal_string:组队训练比赛记录:缓冲区 [2021/10/04 17:00] (当前版本) jxm2001 |
||
---|---|---|---|
行 1: | 行 1: | ||
- | ===== K. Walking ===== | + | ===== H. travel ===== |
==== 题意 ==== | ==== 题意 ==== | ||
- | 给定一个 $n\times m$ 的网格和初始点 $(a,b)$,求从初始点出发移动 $t$ 步且始终不出界的情况下的所有走法。 | + | 给定一棵点权树,从树上选三条不相交的路径,每条路径的权值定义为路径上的点权和,要求最大化三条路径权值和。 |
==== 题解 ==== | ==== 题解 ==== | ||
- | 显然横轴坐标是独立的,可以分开考虑。 | + | 设 $\text{dp}(u,0/1/2,i)$ 表示只考虑 $u$ 的子树,结点 $u$ 的状态为 $0/1/2$ 时,已经选中了 $i$ 条链此时的最大路径权值和。 |
- | 设 $f(s,n,a)$ 表示从一维坐标轴从 $a$ 点出发走 $s$ 步且始终处于 $[1,n]$ 范围内的情况下的所有走法。于是答案为 | + | 我们需要维护一条正在生成的链,这条链不包含在已经选中的 $i$ 条链当中,如果 $u$ 状态为 $0$ 表示 $u$ 不在生成链中。 |
+ | |||
+ | 如果 $u$ 状态为 $1$ 表示 $u$ 在生成链中且 $u$ 只有一个儿子在生成链中, $u$ 状态为 $2$ 表示 $u$ 在生成链中且 $u$ 有两个儿子在生成链中。 | ||
+ | |||
+ | 考虑状态转移,利用生成链的合并,不难有 | ||
$$ | $$ | ||
- | \sum_{i=0}^t {t\choose i}f(i,n,a)f(t-i,m,b) | + | \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) | ||
$$ | $$ | ||
- | 接下来考虑如何计算 $f(s,n,a)(s\in [0,t])$,$f(s,m,b)$ 的计算方式类同。 | + | 注意上式的 $\gets$ 表示取最大值,另外为了防止选中复数条从 $v$ 生成的链,需要开一个临时数组存储中间量。 |
- | 方案一:设 $\text{dp}(i,j)$ 表示走 $i$ 步最后位于 $j$ 点且始终为出界的方案数,不难得到一个 $O(nt)$ 的暴力解法。 | + | 初始状态为 $\text{dp}(u,1,0)=a_u$,最后转移完要考虑将正在生成的链转化为已经选中的链,于是有 |
- | + | ||
- | 方案二:不难发现,有 | + | |
$$ | $$ | ||
- | \begin{equation}\begin{split} | + | \text{dp}(u,0,i)\gets \max(\text{dp}(u,1,i-1),\text{dp}(u,2,i-1)) |
- | f(s,n,a)&=\sum_{i=1}^n \text{dp}(s,i) \\ | + | |
- | &=\text{dp}(s-1,1)+\sum_{i=2}^{n-1} (\text{dp}(s-1,i-1)+\text{dp}(s-1,i+1))+\text{dp}(s-1,n)\\ | + | |
- | & =2f(s-1,n,a)-\text{dp}(s-1,1)-\text{dp}(s-1,n) | + | |
- | \end{split}\end{equation} | + | |
$$ | $$ | ||
- | 于是问题转化为计算 $\text{dp}(s,1),\text{dp}(s,n)$。 | + | 最终答案为 $\text{dp}(1,0,3)$,时间复杂度 $O(nk^2)$,其中 $k$ 表示最多能选中的链的个数。 |
+ | [[http://tokitsukaze.live/2018/07/24/2018niuke2.H/|参考资料]] | ||
<hidden 查看代码> | <hidden 查看代码> | ||
<code cpp> | <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> | </code> | ||
</hidden> | </hidden> |