题解:P17878 中位树 / median
Kayisama
·
·
题解
好题呀好题,赛时差点切掉,来交一篇题解。
有一个经典 trick,在 P2824 等题目中均有体现,貌似最初出处在某场 AGC 里面。
我们先来思考一个问题:对于给定的 n 个数 a_1 到 a_n,我们有什么方式求它的中位数?
一个很简单的方式就是排序然后直接求,但是存在一个很离谱的做法是这样的:我们二分中位数 \text{mid},然后令序列中所有大于等于 \text{mid} 的数为 1,所有小于 \text{mid} 的数为 -1,那么我们就能通过这个新序列的和来判定我们当前二分的这个中位数是大了还是小了,因而慢慢找到真正的中位数。
换到这个题上面,我们考虑二分答案,并且对于一种划分方式所产生的若干连通块的价值像上面那个问题一样转化为 1 和 -1 后再做,这样子我们就只需要最大化连通块转化后的价值和。
既然要求最大化,那么很显然有贪心和 DP 两条路可以走,而因为影响的因素太多且存在后效性,这题只能用 DP。我们考虑最朴素的状态设计:
f_{u,0/1,\text{cnt},\text{sum}}
这个东西的复杂度是 $O(n^2 \times (\sum |a_i|) \times \log n)$ 的,显然无法通过本题,于是我们思考是不是可以简化什么。
我们可以注意到,一个状态 $u$ 比另一个状态 $v$ 更优当且仅当 $\text{cnt}_u>\text{cnt}_v$ 或者 $\text{cnt}_u=\text{cnt}_v \land \text{sum}_u>\text{sum}_v$,于是我们可以贪心的去处理这两维,把它们放进 $f$ 的值的部分,即令 $f_{u,0/1}=(\text{cnt}_{u,0/1},\text{sum}_{u,0/1})$。对于同一个 $f_{u,0/1}$,我们直接贪心的取更优的那个即可。
然后就是两个状态之间的合并:
对于不断边的情况,我们注意到如果两个状态的断边余额不同,则合并后必定会存在一个余额,否则不会存在余额(余额为 $2$ 的时候令 $k$ 减少一则变为没有余额,之后要用可以再加上),故我们应该把 $f_{u,i}$ 和 $f_{v,j}$ 转移到 $f_{u,i \operatorname{xor} j}$,具体地,因为是直接合并,所以令 $f_{u,i \operatorname{xor} j}$ 与 $(\text{cnt}_u+\text{cnt}_v,\text{sum}_u+\text{sum}_v)$ 取 max。
对于断边的情况,则必定是不断边情况下剩余余额异或一,而由于断边,$v$ 所在连通块失去了扩展的可能,于是转移则需要用 $\text{sum}_v$ 计算出它对 $\text{cnt}_u$ 的贡献并加进去,而新的 $\text{sum}_u$ 因为连通块没有扩展所以没有变化,具体的,令 $f_{u,i \operatorname{xor} j \operatorname{xor} 1}$ 与 $(\text{cnt}_u+\text{cnt}_v+\operatorname{sgn}(\text{sum}_v),\text{sum}_u)$ 取 max,其中 $\operatorname{sgn}(x)=[x \ge \text{mid}]*1+[x < \text{mid}]*(-1)$,“[]”为艾弗森括号。
$f_{u,0}$ 最初因为只有一个点所以是 $(0,a_u)$,$f_{u,1}$ 最初因为不可能存在所以是 $(-\infty,-\infty)$。转移的时候需要额外开一个数组来临时记录被修改的 $f_u$,因为 $f_u$ 需要参与到转移的后续计算中去,需要原版的。
最后在根节点 $1$ 处,只要 $\text{cnt}_{1, 0} + \operatorname{sgn}(\text{sum}_{1, 0}) \ge 1$(根节点所在连通块不可能扩展,贡献加入 $\text{cnt}_1$)即说明 $\text{mid}$ 合法。
复杂度 $O(n\log (\sum |a_i|))$,足以通过本题。
:::success[一点证明]
大家可能会疑惑会不会出现 $\text{cnt}$ 更小但 $\text{sum}$ 特别大的状态在后面反超的情况。事实上,同一节点、同奇偶性下的两个状态,若 $\text{cnt}_A > \text{cnt}_B$,由于每多切出一个连通块都会使 $\pm 1$ 总和的奇偶性翻转一次,两者同奇偶性下必有 $\text{cnt}_A - \text{cnt}_B \ge 2$;而当前根连通块在最终划分中最多只能贡献 $+1$ 或 $-1$(两者最大差距只有 $2$),所以 $\text{cnt}_B$ 即使 $\text{sum}_B$ 再大,后续最多也只能打平,绝不可能反超 $\text{cnt}_A$,因此直接按 $(\text{cnt}, \text{sum})$ 字典序贪心取最优的正确性是没有问题的。
:::
代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
namespace MySpace{
#define lowbit(x) (x&(-x))
#define sqrt sqrtl
using pii=pair<int,int>;
#define whileu(p) while (p--)
#define forl(a,b,c) for (auto a=b;a<=c;a=a+1)
inline void read() {}
template <typename T, typename... R>
inline void read(T &x,R &... oth){
x=0;T f=1;
char c=getchar();
while(c<'0' || c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c&15),c=getchar();
x*=f;
read(oth...);
return;
}
template<typename T>
T qpow(T a,T n,T p){
T res=1;a%=p;
while (n){
if (n&1) res=1ll*res*a%p;
a=1ll*a*a%p;
n>>=1;
}
return res;
}
template<typename T>
T gcd(T a,T b){return (b>0?gcd(b,a%b):a);}
template<typename T>
T exgcd(T a,T b,T& x,T& y){
if (b<=0ll){
x=1ll,y=0ll;
return a;
}
T d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return d;
}
template<typename T>
T inv(T a,T p){
if (a==1) return 1ll;
T x,y;
if (exgcd(a,p,x,y)!=1ll) return -1ll;
else return (x+p)%p;
}
}
using namespace MySpace;
#define int long long
const int maxn=1e5+5;
const int inf=4e18;
int T;
int n;
int a[maxn];
vector<int> e[maxn];
struct node{
int c,sum;
};
node f[maxn][2];
int mid;
node max(node a,node b){
if (a.c==b.c) return a.sum>b.sum?a:b;
else return a.c>b.c?a:b;
}
void dfs(int u,int fa=0){
f[u][0]={0,a[u]};
f[u][1]={-maxn,-inf};
for (auto v:e[u]){
if (v==fa) continue;
dfs(v,u);
node nf[2]={{-maxn,-inf},{-maxn,-inf}};
for (int i=0;i<=1;i++){
if (f[u][i].c<=-maxn) continue;
for (int j=0;j<=1;j++){
if (f[v][j].c<=-maxn) continue;
node x=f[u][i],y=f[v][j];
nf[i^j]=max(nf[i^j],{x.c+y.c,x.sum+y.sum});
nf[i^j^1]=max(nf[i^j^1],{x.c+y.c+(y.sum>=mid?1:-1),x.sum});
}
}
f[u][0]=nf[0],f[u][1]=nf[1];
}
return;
}
signed main(){
read(T,T);
while (T--){
read(n);
for (int i=1;i<=n;i++) e[i].clear();
for (int i=1;i<=n;i++) read(a[i]);
for (int i=1;i<n;i++){
int u,v;read(u,v);
e[u].push_back(v);
e[v].push_back(u);
}
int l=-1e18,r=1e18;
while (l<r){
mid=(l+r+1)>>1;
dfs(1);
if (f[1][0].c+(f[1][0].sum>=mid?1:-1)>=1) l=mid;
else r=mid-1;
}
printf("%lld\n",l);
}
return 0;
}
```