题解:P17878 中位树 / median

· · 题解

题目传送门

小广告

题意分析

考虑这个东西不能直接 DP,因为你无法从 x 的子节点 v_1,v_2,\cdots,v_k 的子树的中位数得到 x 子树的中位数,中位数可能会在加值的过程中乱跳。

就和幸运数字一样,考虑枚举答案。但是 \mathcal O(V) 直接枚举显然是炸完了的,考虑枚举中位数的下界 \textit{mid},每次只需要判断能否存在最终答案 \textit{ans}\geq\textit{mid},于是具有单调性,可以二分答案。

接下来考虑判定中位数 \geq\textit{mid},考虑树形 DP。

对于一个连通块,记其和为 \textit{sum},如果 \textit{sum}\geq\textit{mid},那么令其贡献为 1,否则为 -1。形式化地:

\operatorname{sgn}(x)= \begin{cases} 1&x\geq\textit{mid}\\ -1&x<\textit{mid} \end{cases}

记所有连通块的贡献和为 s,如果 s>0 且 s 为奇数,那么代表 \textit{mid}\leq\textit{ans},原因显然。

之后就对着这个 DP,考虑子树 x,子树 x 内的某一个连通块想要接到 x 外面去就只能通过 x 连出去,所以单独维护 x 所在连通块的权值和。

记 f_{x,i} 表示 x 子树内,与 x 断开的连通块贡献和为 i 时,x 所在连通块的最大权值和(显然权值和越大越好);i 可以是负数,维护的时候需要加上 n 的偏移量。

考虑 $x$ 的子节点 $v$ 的转移: * $(x,v)$ 不断开。 此时 $v$ 所在连通块会接到 $x$ 上,于是有: $$ f'_{x,i+j}\leftarrow\max(f'_{x,i+j},f_{x,i}+f_{v,j}) $$ * $(x,v)$ 断开。 此时 $v$ 所在连通块需要封闭。记 $\displaystyle k=\max_j(j+\operatorname{sgn}(f_{v,j}))$,有: $$ f'_{x,i+k}\leftarrow\max(f'_{x,i+k},f_i) $$ 复杂度就是树上背包,$\mathcal O\left(n^2\log V\right)$。 :::info[$\mathcal O\left(n^2\log V\right)$ 参考代码] ```cpp //#include<bits/stdc++.h> #include<algorithm> #include<iostream> #include<cstring> #include<iomanip> #include<cstdio> #include<string> #include<vector> #include<cmath> #include<ctime> #include<deque> #include<queue> #include<stack> #include<list> using namespace std; typedef long long ll; constexpr const int N=800; constexpr const ll inf=0x3f3f3f3f3f3f3f3f; int n,size[N+1]; ll w[N+1]; vector<int>g[N+1]; ll f[N+1][N<<1|1]; void dfs(int x,int fx,ll mid){ for(int i=-n;i<=n;i++){ f[x][i+n]=-inf; } size[x]=1; f[x][0+n]=w[x]; for(int v:g[x]){ if(v==fx){ continue; } dfs(v,x,mid); int Max[2]={-n-1,-n-1}; for(int j=-size[v];j<=size[v];j++){ if(f[v][j+n]==-inf){ continue; } int s=j+(f[v][j+n]>=mid?1:-1); Max[s&1]=max(Max[s&1],s); } static ll fx[N<<1|1]; for(int i=-n;i<=n;i++){ fx[i+n]=f[x][i+n]; f[x][i+n]=-inf; } for(int i=-size[x];i<=size[x];i++){ if(fx[i+n]==-inf){ continue; } if(Max[0]!=-n-1){ f[x][i+Max[0]+n]=max(f[x][i+Max[0]+n],fx[i+n]); } if(Max[1]!=-n-1){ f[x][i+Max[1]+n]=max(f[x][i+Max[1]+n],fx[i+n]); } for(int j=-size[v];j<=size[v];j++){ if(f[v][j+n]==-inf){ continue; } f[x][i+j+n]=max(f[x][i+j+n],fx[i+n]+f[v][j+n]); } } size[x]+=size[v]; } } bool check(ll mid){ dfs(1,0,mid); for(int i=-size[1];i<=size[1];i++){ if(f[1][i+n]==-inf){ continue; } int s=i+(f[1][i+n]>=mid?1:-1); if(s>0&&(s&1)){ return true; } } return false; } int main(){ /*freopen("test.in","r",stdin); freopen("test.out","w",stdout);*/ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int c,T; cin>>c>>T; while(T--){ cin>>n; for(int i=1;i<=n;i++){ cin>>w[i]; g[i].resize(0); } for(int i=1;i<n;i++){ int u,v; cin>>u>>v; g[u].push_back(v); g[v].push_back(u); } ll l=0,r=0; for(int i=1;i<=n;i++){ l+=min(w[i],0ll); r+=max(w[i],0ll); } ll ans=-inf; while(l<=r){ ll mid=l+r>>1; if(check(mid)){ ans=mid; l=mid+1; }else{ r=mid-1; } } cout<<ans<<'\n'; } cout.flush(); /*fclose(stdin); fclose(stdout);*/ return 0; } ``` ::: 考虑如何优化,~~因为看起来很有前途~~。 手玩一下,可以发现一个点有用的状态不多,只有 $\mathcal O(1)$ 个。 因为答案需要是奇数,所以分奇偶性维护 DP 状态,记 $g_{x,0/1}$ 表示 $x$ 子树内除开 $x$ 所在连通块,贡献之和奇偶性为 $0,1$ 时的最大总贡献。 实际上,和 $g_{x,0},g_{x,1}$ 的贡献相差 $>1$ 的状态是无用的,因为至多从 $x$ 所在连通块补回来 $1$ 的贡献。分奇偶性维护就解决了这个问题。 所以有用的 $f_{x,i}$ 也不多。记 $\operatorname{sum}(x)$ 表示 $x$ 所在连通块的权值和,重新定义 $f_{x,i\in\set{0,1},j\in\set{-1,1}}$ 表示对于 $g_{x,i}$,总贡献 $\operatorname{sgn}(\operatorname{sum}(x))=j$ 时的最大 $\operatorname{sum}(x)$。 就可以直接用这个转移了,每个点都只有 $\mathcal O(1)$ 个状态需要转移,但是常数略大。 实际实现上需要注意负数下标的偏移量问题。 # AC 代码 卡常,`vector` 换成 `basic_string` 果然快了很多。 ```cpp //#include<bits/stdc++.h> #include<algorithm> #include<iostream> #include<cstring> #include<iomanip> #include<cstdio> #include<string> #include<vector> #include<cmath> #include<ctime> #include<deque> #include<queue> #include<stack> #include<list> using namespace std; namespace IO{ constexpr auto IO_maxn=1<<20;char IO_in[IO_maxn],IO_out[IO_maxn],*IO_p1=IO_in,*IO_p2=IO_in,*IO_p3=IO_out; inline void IO_flush(){fwrite(IO_out,1,IO_p3-IO_out,stdout);} inline char IO_getch(){return (IO_p1==IO_p2&&(IO_p2=(IO_p1=IO_in)+fread(IO_in,1,IO_maxn,stdin),IO_p1==IO_p2)?EOF:*IO_p1++);} inline void IO_putch(const char &IO_x){(IO_p3==IO_out+IO_maxn&&(IO_flush(),IO_p3=IO_out),*IO_p3++=(IO_x));} template<typename type>inline type IO_read(type &IO_x){IO_x=0;bool IO_flag(0);char IO_ch=IO_getch();while(!isdigit(IO_ch)){IO_flag^=IO_ch=='-',IO_ch=IO_getch();}while(isdigit(IO_ch)){IO_x=(IO_x<<1)+(IO_x<<3)+(IO_ch^48),IO_ch=IO_getch();}return IO_flag?IO_x=-IO_x:IO_x;} inline char IO_read(char &IO_x){do IO_x=IO_getch();while(isspace(IO_x));return IO_x;} inline void IO_read(char *IO_x){static char IO_ch;IO_read(IO_ch);do{*(IO_x++)=IO_ch;}while(!isspace(IO_ch=IO_getch())&&~IO_ch);} inline void IO_read(std::string &IO_x){static char IO_ch;IO_read(IO_ch),IO_x.clear();do{IO_x+=IO_ch;}while(!isspace(IO_ch=IO_getch())&&~IO_ch);} inline void IO_write(const char &IO_x){IO_putch(IO_x);} template<typename type>inline void IO_write(type *IO_x){while(*IO_x)IO_putch(*(IO_x++));} inline void IO_write(const std::string &IO_x){for(register int IO_i=0,IO_len=IO_x.length();IO_i<IO_len;++IO_i)IO_putch(IO_x[IO_i]);} template<typename type>inline void IO_write(type IO_x){if(IO_x<0)IO_x=-IO_x,IO_putch('-');static short IO_Stack[50],IO_top(0);do{IO_Stack[++IO_top]=IO_x%10,IO_x/=10;}while(IO_x);while(IO_top){IO_putch(IO_Stack[IO_top--]|48);}} struct IO_Basic{ template<typename type>IO_Basic &operator>>(type &IO_x){IO_x=IO_read(IO_x);return (*this);} template<typename type>IO_Basic &operator<<(const type &IO_x){IO_write(IO_x);return (*this);} }IO_Cin,IO_Cout; class Flush{public:~Flush(){IO_flush();}}_; }using namespace IO; #define cin IO_Cin #define cout IO_Cout typedef long long ll; constexpr const int N=1e5; constexpr const ll inf=0x3f3f3f3f3f3f3f3f; int n,size[N+1]; ll w[N+1]; basic_string<int>g[N+1]; struct DP{ int g[2]; ll f[2][2]; }f[N+1]; void dfs(int x,int fx,ll mid){ f[x].g[0]=-n-1; f[x].g[1]=(w[x]>=mid?1:-1); f[x].f[0][0]=f[x].f[0][1]=f[x].f[1][0]=f[x].f[1][1]=-inf; f[x].f[1][w[x]<mid]=w[x]; for(int v:g[x]){ if(v==fx){ continue; } dfs(v,x,mid); basic_string<pair<int,ll>>a; a.reserve(24); for(int p=0;p<=1;p++){ if(f[x].g[p]==-n-1){ continue; } for(int j=0;j<=1;j++){ if(f[x].f[p][j]==-inf){ continue; } int as=f[x].g[p]+2*j-1; ll av=f[x].f[p][j]; for(int q=0;q<=1;q++){ if(f[v].g[q]==-n-1){ continue; } a.push_back({as+f[v].g[q],av}); for(int k=0;k<=1;k++){ if(f[v].f[q][k]==-inf){ continue; } a.push_back({as+f[v].g[q]+2*k-1,av+f[v].f[q][k]}); } } } } f[x].g[0]=f[x].g[1]=-n-1; f[x].f[0][0]=f[x].f[0][1]=f[x].f[1][0]=f[x].f[1][1]=-inf; for(auto [as,av]:a){ int s=as+(av>=mid?1:-1); f[x].g[s&1]=max(f[x].g[s&1],s); } for(auto [as,av]:a){ int p=(as+1)&1; if(as==f[x].g[p]-1){ f[x].f[p][0]=max(f[x].f[p][0],av); }else if(as==f[x].g[p]+1){ f[x].f[p][1]=max(f[x].f[p][1],av); } } } } bool check(ll mid){ dfs(1,0,mid); return f[1].g[1]>0; } int main(){ /*freopen("test.in","r",stdin); freopen("test.out","w",stdout);*/ // ios::sync_with_stdio(false); // cin.tie(0);cout.tie(0); int c,T; cin>>c>>T; while(T--){ cin>>n; for(int i=1;i<=n;i++){ cin>>w[i]; g[i].resize(0); } for(int i=1;i<n;i++){ int u,v; cin>>u>>v; g[u].push_back(v); g[v].push_back(u); } ll l=0,r=0; for(int i=1;i<=n;i++){ l+=min(w[i],0ll); r+=max(w[i],0ll); } ll ans=-inf; while(l<=r){ ll mid=l+r>>1; if(check(mid)){ ans=mid; l=mid+1; }else{ r=mid-1; } } cout<<ans<<'\n'; } // cout.flush(); /*fclose(stdin); fclose(stdout);*/ return 0; } ```