令 f u , i f_{u,i} f u , i 表示子树 u u u 内选择 i i i 个黑点的方案数
设要把 v v v 并入 u u u ,原本的 f u f_u f u 记为 f f f ,f v f_v f v 记为 g g g ,合并后的 f u f_u f u 记为 h h h ,则转移为
h x ← min a + b = x ( f a + g b + ∣ 2 b − k ∣ ) h_x\gets\min_{a+b=x}(f_a+g_b+|2b-k|) h x ← a + b = x min ( f a + g b + ∣2 b − k ∣ )
保存 f u , ∗ f_{u,\ast} f u , ∗ 的差分数组 f u , ∗ ′ f'_{u,\ast} f u , ∗ ′ ,则 g b ← g b + ∣ 2 b − k ∣ g_b\gets g_b+|2b-k| g b ← g b + ∣2 b − k ∣ 等价于 g 0 ′ ← g 0 ′ + k g'_0\gets g'_0+k g 0 ′ ← g 0 ′ + k ,g 1 ∼ ⌊ k 2 ⌋ ′ g'_{1\sim \lfloor\frac k2\rfloor} g 1 ∼ ⌊ 2 k ⌋ ′ 减去 2 2 2 ,g ⌈ k 2 ⌉ + 1 ∼ s z ( v ) ′ g'_{\lceil\frac k2\rceil+1\sim sz(v)} g ⌈ 2 k ⌉ + 1 ∼ sz ( v ) ′ 加上 2 2 2
每个 f u , ∗ f_{u,\ast} f u , ∗ 用两个可并堆维护,一个保存前 k 2 \frac k2 2 k 个值,一个保存剩余值
时间复杂度 O ( n log n ) O(n\log n) O ( n log n )
代码
参考
存在相同的 a a a 时显然行列式为 0 0 0
交换一对 a i , a j ∣ i ≠ j a_i,a_j\mid i\ne j a i , a j ∣ i = j 显然不改变行列式的值,因此可以任意重排 a a a ,假定按 dfs \text{dfs} dfs 序排列
令 f u f_u f u 表示取 a a a 在子树 u u u 内的区间计算出的行列式的值
考虑加入一个儿子 v v v 后对 f f f 的影响
用 ( v ) a × b (v)_{a\times b} ( v ) a × b 表示大小为 a × b a\times b a × b 的全 v v v 矩阵,用 M [ a , b ] M_{[a,b]} M [ a , b ] 表示矩阵 M M M 第 a a a 至 b b b 行的部分,空白表示这部分为 0 0 0
设加入 v v v 前 f u f_u f u 对应的方阵为 A x × x A_{x\times x} A x × x ,v v v 对应方阵为 D y × y D_{y\times y} D y × y ,令 p p p 为结点 u u u 的权值,则加入 v v v 后的矩阵为 [ A ( p ) x × y ( p ) y × x D ] \begin{bmatrix}A&(p)_{x\times y}\\(p)_{y\times x}&D\end{bmatrix} [ A ( p ) y × x ( p ) x × y D ]
显然
det [ A ( p ) x × y ( p ) y × x D ] = det [ A ( p ) x × y ( p ) ( y − 1 ) × x D [ 1 , y − 1 ] ( p ) 1 × x D [ y , y ] ] = det [ A ( p ) x × y ( p ) ( y − 1 ) × x D [ 1 , y − 1 ] D [ y , y ] − ( p ) 1 × y ] + det [ A ( p ) x × y ( p ) ( y − 1 ) × x D [ 1 , y − 1 ] ( p ) 1 × x ( p ) 1 × y ] \begin{aligned}
&\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{y\times x}&D\end{bmatrix}\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{(y-1)\times x}&D_{[1,y-1]}\\(p)_{1\times x}&D_{[y,y]}\end{bmatrix}\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{(y-1)\times x}&D_{[1,y-1]}\\&D_{[y,y]}-(p)_{1\times y}\end{bmatrix}
+\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{(y-1)\times x}&D_{[1,y-1]}\\(p)_{1\times x}&(p)_{1\times y}\end{bmatrix}\\
\end{aligned} = = det [ A ( p ) y × x ( p ) x × y D ] det A ( p ) ( y − 1 ) × x ( p ) 1 × x ( p ) x × y D [ 1 , y − 1 ] D [ y , y ] det A ( p ) ( y − 1 ) × x ( p ) x × y D [ 1 , y − 1 ] D [ y , y ] − ( p ) 1 × y + det A ( p ) ( y − 1 ) × x ( p ) 1 × x ( p ) x × y D [ 1 , y − 1 ] ( p ) 1 × y
第二个
det [ A ( p ) x × y ( p ) ( y − 1 ) × x D [ 1 , y − 1 ] ( p ) 1 × x ( p ) 1 × y ] = det [ A [ 1 , 1 ] ( p ) 1 × y A [ 2 , 2 ] ( p ) 1 × y ⋮ ⋮ A [ x , x ] ( p ) 1 × y ( p ) 1 × x D [ 1 , 1 ] ( p ) 1 × x D [ 2 , 2 ] ⋮ ⋮ ( p ) 1 × x D [ y − 1 , y − 1 ] ( p ) 1 × x ( p ) 1 × y ] = det [ A [ 1 , 1 ] − ( p ) 1 × x A [ 2 , 2 ] − ( p ) 1 × x ⋮ A [ x , x ] − ( p ) 1 × x D [ 1 , 1 ] − ( p ) 1 × y D [ 2 , 2 ] − ( p ) 1 × y ⋮ D [ y − 1 , y − 1 ] − ( p ) 1 × y ( p ) 1 × x ( p ) 1 × y ] = det [ A − ( p ) x × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( p ) 1 × x ( p ) 1 × y ] = p det [ A − ( p ) x × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( 1 ) 1 × x ( 1 ) 1 × y ] = p det [ A − ( p ) x × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( 1 ) 1 × y ] \begin{aligned}
&\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{(y-1)\times x}&D_{[1,y-1]}\\(p)_{1\times x}&(p)_{1\times y}\end{bmatrix}\\
=&\det \begin{bmatrix}
A_{[1,1]}&(p)_{1\times y}\\
A_{[2,2]}&(p)_{1\times y}\\
\vdots&\vdots\\
A_{[x,x]}&(p)_{1\times y}\\
(p)_{1\times x}&D_{[1,1]}\\
(p)_{1\times x}&D_{[2,2]}\\
\vdots&\vdots\\
(p)_{1\times x}&D_{[y-1,y-1]}\\
(p)_{1\times x}&(p)_{1\times y}\end{bmatrix}\\
=&\det \begin{bmatrix}
A_{[1,1]}-(p)_{1\times x}&\\
A_{[2,2]}-(p)_{1\times x}&\\
\vdots&\\
A_{[x,x]}-(p)_{1\times x}&\\
&D_{[1,1]}-(p)_{1\times y}\\
&D_{[2,2]}-(p)_{1\times y}\\
&\vdots\\
&D_{[y-1,y-1]}-(p)_{1\times y}\\
(p)_{1\times x}&(p)_{1\times y}\end{bmatrix}\\
=&\det \begin{bmatrix}
A-(p)_{x\times x}&\\
&D_{[1,y-1]}-(p)_{(y-1)\times y}\\
(p)_{1\times x}&(p)_{1\times y}\end{bmatrix}\\
=&p\det \begin{bmatrix}
A-(p)_{x\times x}&\\
&D_{[1,y-1]}-(p)_{(y-1)\times y}\\
(1)_{1\times x}&(1)_{1\times y}\end{bmatrix}\\
=&p\det \begin{bmatrix}
A-(p)_{x\times x}&\\
&D_{[1,y-1]}-(p)_{(y-1)\times y}\\
&(1)_{1\times y}\end{bmatrix}\\
\end{aligned} = = = = = det A ( p ) ( y − 1 ) × x ( p ) 1 × x ( p ) x × y D [ 1 , y − 1 ] ( p ) 1 × y det A [ 1 , 1 ] A [ 2 , 2 ] ⋮ A [ x , x ] ( p ) 1 × x ( p ) 1 × x ⋮ ( p ) 1 × x ( p ) 1 × x ( p ) 1 × y ( p ) 1 × y ⋮ ( p ) 1 × y D [ 1 , 1 ] D [ 2 , 2 ] ⋮ D [ y − 1 , y − 1 ] ( p ) 1 × y det A [ 1 , 1 ] − ( p ) 1 × x A [ 2 , 2 ] − ( p ) 1 × x ⋮ A [ x , x ] − ( p ) 1 × x ( p ) 1 × x D [ 1 , 1 ] − ( p ) 1 × y D [ 2 , 2 ] − ( p ) 1 × y ⋮ D [ y − 1 , y − 1 ] − ( p ) 1 × y ( p ) 1 × y det A − ( p ) x × x ( p ) 1 × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( p ) 1 × y p det A − ( p ) x × x ( 1 ) 1 × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( 1 ) 1 × y p det A − ( p ) x × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( 1 ) 1 × y
第一个
det [ A ( p ) x × y ( p ) ( y − 1 ) × x D [ 1 , y − 1 ] D [ y , y ] − ( p ) 1 × y ] = det [ A ( p ) x × y ( p ) ( y − 2 ) × x D [ 1 , y − 2 ] ( p ) 1 × x D [ y − 1 , y − 1 ] D [ y , y ] − ( p ) 1 × y ] = det [ A ( p ) x × y ( p ) ( y − 2 ) × x D [ 1 , y − 2 ] D [ y − 1 , y − 1 ] − ( p ) 1 × y D [ y , y ] − ( p ) 1 × y ] + det [ A ( p ) x × y ( p ) ( y − 2 ) × x D [ 1 , y − 2 ] ( p ) 1 × x ( p ) 1 × y D [ y , y ] − ( p ) 1 × y ] = det [ A ( p ) x × y ( p ) ( y − 2 ) × x D [ 1 , y − 2 ] D [ y − 1 , y ] − ( p ) 2 × y ] + p det [ A − ( p ) x × x D [ 1 , y − 2 ] − ( p ) ( y − 2 ) × y ( 1 ) 1 × y D [ y , y ] − ( p ) 1 × y ] \begin{aligned}
&\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{(y-1)\times x}&D_{[1,y-1]}\\&D_{[y,y]}-(p)_{1\times y}\end{bmatrix}\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\
(p)_{(y-2)\times x}&D_{[1,y-2]}\\
(p)_{1\times x}&D_{[y-1,y-1]}\\
&D_{[y,y]}-(p)_{1\times y}
\end{bmatrix}\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\
(p)_{(y-2)\times x}&D_{[1,y-2]}\\
&D_{[y-1,y-1]}-(p)_{1\times y}\\
&D_{[y,y]}-(p)_{1\times y}
\end{bmatrix}
+\det \begin{bmatrix}A&(p)_{x\times y}\\
(p)_{(y-2)\times x}&D_{[1,y-2]}\\
(p)_{1\times x}&(p)_{1\times y}\\
&D_{[y,y]}-(p)_{1\times y}
\end{bmatrix}\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\
(p)_{(y-2)\times x}&D_{[1,y-2]}\\
&D_{[y-1,y]}-(p)_{2\times y}\\
\end{bmatrix}
+p\det \begin{bmatrix}A-(p)_{x\times x}&\\
&D_{[1,y-2]}-(p)_{(y-2)\times y}\\
&(1)_{1\times y}\\
&D_{[y,y]}-(p)_{1\times y}
\end{bmatrix}\\
\end{aligned} = = = det A ( p ) ( y − 1 ) × x ( p ) x × y D [ 1 , y − 1 ] D [ y , y ] − ( p ) 1 × y det A ( p ) ( y − 2 ) × x ( p ) 1 × x ( p ) x × y D [ 1 , y − 2 ] D [ y − 1 , y − 1 ] D [ y , y ] − ( p ) 1 × y det A ( p ) ( y − 2 ) × x ( p ) x × y D [ 1 , y − 2 ] D [ y − 1 , y − 1 ] − ( p ) 1 × y D [ y , y ] − ( p ) 1 × y + det A ( p ) ( y − 2 ) × x ( p ) 1 × x ( p ) x × y D [ 1 , y − 2 ] ( p ) 1 × y D [ y , y ] − ( p ) 1 × y det A ( p ) ( y − 2 ) × x ( p ) x × y D [ 1 , y − 2 ] D [ y − 1 , y ] − ( p ) 2 × y + p det A − ( p ) x × x D [ 1 , y − 2 ] − ( p ) ( y − 2 ) × y ( 1 ) 1 × y D [ y , y ] − ( p ) 1 × y
从而最初的
det [ A ( p ) x × y ( p ) y × x D ] = det [ A ( p ) x × y ( p ) ( y − 2 ) × x D [ 1 , y − 2 ] D [ y − 1 , y ] − ( p ) 2 × y ] + p det [ A − ( p ) x × x D [ 1 , y − 2 ] − ( p ) ( y − 2 ) × y ( 1 ) 1 × x ( 1 ) 1 × y D [ y , y ] − ( p ) 1 × y ] + p det [ A − ( p ) x × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( 1 ) 1 × y ] = ⋯ = det [ A ( p ) x × y D − ( p ) y × y ] + p ∑ i = 1 y det [ A − ( p ) x × x D [ 1 , i − 1 ] − ( p ) ( i − 1 ) × y ( 1 ) 1 × y D [ i + 1 , y ] − ( p ) y − i × y ] = det A det ( D − ( p ) y × y ) + p det ( A − ( p ) x × x ) ∑ i = 1 y det [ D [ 1 , i − 1 ] − ( p ) ( i − 1 ) × y ( 1 ) 1 × y D [ i + 1 , y ] − ( p ) y − i × y ] \begin{aligned}
&\det \begin{bmatrix}A&(p)_{x\times y}\\(p)_{y\times x}&D\end{bmatrix}\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\
(p)_{(y-2)\times x}&D_{[1,y-2]}\\
&D_{[y-1,y]}-(p)_{2\times y}\\
\end{bmatrix}
+p\det \begin{bmatrix}A-(p)_{x\times x}&\\
&D_{[1,y-2]}-(p)_{(y-2)\times y}\\
(1)_{1\times x}&(1)_{1\times y}\\
&D_{[y,y]}-(p)_{1\times y}
\end{bmatrix}
+p\det \begin{bmatrix}
A-(p)_{x\times x}&\\
&D_{[1,y-1]}-(p)_{(y-1)\times y}\\
&(1)_{1\times y}\end{bmatrix}\\
=&\cdots\\
=&\det \begin{bmatrix}A&(p)_{x\times y}\\
&D-(p)_{y\times y}\\
\end{bmatrix}
+p\sum_{i=1}^y
\det \begin{bmatrix}A-(p)_{x\times x}&\\
&D_{[1,i-1]}-(p)_{(i-1)\times y}\\
&(1)_{1\times y}\\
&D_{[i+1,y]}-(p)_{y-i\times y}
\end{bmatrix}\\
=&\det A \det(D-(p)_{y\times y})
+p
\det(A-(p)_{x\times x})
\sum_{i=1}^y\det \begin{bmatrix}
D_{[1,i-1]}-(p)_{(i-1)\times y}\\
(1)_{1\times y}\\
D_{[i+1,y]}-(p)_{y-i\times y}
\end{bmatrix}\\
\end{aligned} = = = = det [ A ( p ) y × x ( p ) x × y D ] det A ( p ) ( y − 2 ) × x ( p ) x × y D [ 1 , y − 2 ] D [ y − 1 , y ] − ( p ) 2 × y + p det A − ( p ) x × x ( 1 ) 1 × x D [ 1 , y − 2 ] − ( p ) ( y − 2 ) × y ( 1 ) 1 × y D [ y , y ] − ( p ) 1 × y + p det A − ( p ) x × x D [ 1 , y − 1 ] − ( p ) ( y − 1 ) × y ( 1 ) 1 × y ⋯ det [ A ( p ) x × y D − ( p ) y × y ] + p i = 1 ∑ y det A − ( p ) x × x D [ 1 , i − 1 ] − ( p ) ( i − 1 ) × y ( 1 ) 1 × y D [ i + 1 , y ] − ( p ) y − i × y det A det ( D − ( p ) y × y ) + p det ( A − ( p ) x × x ) i = 1 ∑ y det D [ 1 , i − 1 ] − ( p ) ( i − 1 ) × y ( 1 ) 1 × y D [ i + 1 , y ] − ( p ) y − i × y
令 g u g_u g u 表示 f u f_u f u 对应行列式中将一行替换为 1 1 1 ,所有 s z u sz_u s z u 种可能得到的行列式的值之和
则加入一个 v v v 时,先将子树 v v v 内所有点的权值减去 p p p ,算出 f v , g v f_v,g_v f v , g v ,然后 f u ← f u × f v + p g v F , g u ← g u f v + F g v f_u\gets f_u\times f_v+pg_vF,g_u\gets g_u f_v+Fg_v f u ← f u × f v + p g v F , g u ← g u f v + F g v ,其中 F F F 为之前所有 f v f_v f v 之积
特殊处理一下 u u u 本身的贡献即可
时间复杂度 O ( n ) O(n) O ( n )
代码
参考
令两种操作代价减半,变成 1 1 1 和 3 3 3 ,最终答案乘以 2 2 2 即可
对于一个确定的序列,令 f i , j f_{i,j} f i , j 表示到第 i i i 个点位置,覆盖前面所有点,代价为 j j j ,向后还能延伸的长度的最大值,转移是容易的
对于一个 i i i ,令 x x x 表示覆盖至 i i i 的最小代价,显然只有 x , f i , x , f i , x + 1 , f i , x + 2 x,f_{i,x},f_{i,x+1},f_{i,x+2} x , f i , x , f i , x + 1 , f i , x + 2 有用,在 d p dp d p 过程中动态统计 x x x 的总贡献,并把 ( f i , x , f i , x + 1 , f i , x + 2 ) (f_{i,x},f_{i,x+1},f_{i,x+2}) ( f i , x , f i , x + 1 , f i , x + 2 ) 记为一个状态
令 f i , ( a , b , c ) f_{i,(a,b,c)} f i , ( a , b , c ) 表示前 i i i 个达到状态 ( a , b , c ) (a,b,c) ( a , b , c ) 的方案数,初始 f 0 , ( 0 , 0 , 0 ) = 1 f_{0,(0,0,0)}=1 f 0 , ( 0 , 0 , 0 ) = 1
对于 f i − 1 , ( a , b , c ) f_{i-1,(a,b,c)} f i − 1 , ( a , b , c ) ,令 v = f i − 1 , ( a , b , c ) v=f_{i-1,(a,b,c)} v = f i − 1 , ( a , b , c ) 为方案数,令 a , b , c a,b,c a , b , c 都减去 t i − t i − 1 t_i-t_{i-1} t i − t i − 1 然后对 0 0 0 取 max \max max ,然后 b ← max ( b , a + 20 ) , c ← max ( c , b + 20 ) b\gets \max(b,a+20),c\gets \max(c,b+20) b ← max ( b , a + 20 ) , c ← max ( c , b + 20 ) ,令 d ← max ( c + 20 , a + 75 ) d\gets \max(c+20,a+75) d ← max ( c + 20 , a + 75 )
若删去当前点,则 f i , a , b , c f_{i,a,b,c} f i , a , b , c 加上 v v v
若不删去,若 a > 0 a>0 a > 0 则最小值不变,f i , a , b , c f_{i,a,b,c} f i , a , b , c 再加上 v v v ,否则最小值加一,将 v × 2 n − i v\times 2^{n-i} v × 2 n − i 计入答案,然后 f i , b , c , d f_{i,b,c,d} f i , b , c , d 加上 v v v
总时间复杂度 O ( n v 3 ) O(nv^3) O ( n v 3 ) ,其中 v = 75 v=75 v = 75 ,常数极小
代码
参考
令 dis ( i , j ) \text{dis}(i,j) dis ( i , j ) 表示环上的距离
从外向内枚举所有给定的边,令 f u f_u f u 表示目前为止走到 u u u 所需额外增加的最小边数,初始 f i = dis ( i , s ) f_i=\text{dis}(i,s) f i = dis ( i , s ) ,设目前加入的边连接 ( x , y ) ∣ y = x m o d n + 1 (x,y)\mid y=x\bmod n+1 ( x , y ) ∣ y = x mod n + 1 ,则交换 f x , f y f_x,f_y f x , f y ,并对于 x , y x,y x , y ,分别更新和向外更新
更新本身时,f x ← min ( f x , f i + dis ( i , x ) ) f_x\gets \min(f_x,f_i+\text{dis}(i,x)) f x ← min ( f x , f i + dis ( i , x )) ,实际上由于除了 f x , f y f_x,f_y f x , f y 外任意 i , j i,j i , j 满足 ∣ f i − f j ∣ ≤ dis ( i , j ) |f_i-f_j|\le \text{dis}(i,j) ∣ f i − f j ∣ ≤ dis ( i , j ) ,因此假设从 x x x 在环上的对面处断开环,对于 x x x 左侧的 i i i ,必有 f i + dis ( i , x ) ≥ f x − 1 + dis ( x − 1 , x ) = f x − 1 + 1 f_i+\text{dis}(i,x)\ge f_{x-1}+\text{dis}(x-1,x)=f_{x-1}+1 f i + dis ( i , x ) ≥ f x − 1 + dis ( x − 1 , x ) = f x − 1 + 1 ,右侧同理,即只需要取 x x x 在环上左右两个位置更新之即可,y y y 同理
向外更新时 f i ← min ( f i , f x + dis ( i , x ) ) f_i\gets \min(f_i,f_x+\text{dis}(i,x)) f i ← min ( f i , f x + dis ( i , x )) ,y y y 同理
向外更新可以拆分为区间 f i f_i f i 对 i − v i-v i − v 取 min \min min 和对 i + v i+v i + v 取 min \min min ,线段树维护即可
总时间复杂度 O ( n + m log n ) O(n+m\log n) O ( n + m log n )
代码
参考