3611: [Heoi2014] Big project | tree DP | virtual tree
Build a virtual tree and then DP calculates the answer
Self-written DP is too silly QAQ, various WA
The DP Method of PoPoQQQ
Mxdis, mndis The distance between the selected vertex closest to the current vertex and the farthest selected vertex, respectively.
Mx, mn The distance between the two points that are farthest and the nearest two points when the current point is the root.
Sum Indicates the sum of all the key distance to the current vertex in the subtree with the current vertex as the root
C Array indicates the number of key points in the subtree with the current vertex as the root
Then I used a timestamp to mark the key point.
#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x7FFFFFFF#define ll long long#define N 1000005using namespace std;int sc(){ int i=0,f=1; char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar(); return i*f;}ll mx[N],mn[N],mn_dis[N],mx_dis[N],c[N],sum[N],ans;int Head[N],Nxt[N],Lst[N];int a[N],st[N],tim[N];int size[N],deep[N],fa[N],S[N],top[N];int head[N],lst[N<<1],nxt[N<<1],v[N<<1];int n,tot,cnt,Tot,TI;void Insert(int x,int y){ Lst[++Tot]=y;Nxt[Tot]=Head[x];Head[x]=Tot;}void insert(int x,int y){ lst[++tot]=y;nxt[tot]=head[x];head[x]=tot; lst[++tot]=x;nxt[tot]=head[y];head[y]=tot;}void dfs(int x,int f){ size[x]=1; for(int i=head[x];i;i=nxt[i]) if(lst[i]!=f) { deep[lst[i]]=deep[x]+1; fa[lst[i]]=x; dfs(lst[i],x); size[x]+=size[lst[i]]; }}void _dfs(int x,int htp){ int k=0;top[x]=htp;S[x]=++cnt; for(int i=head[x];i;i=nxt[i]) if(lst[i]!=fa[x]&&size[lst[i]]>size[k])k=lst[i]; if(!k)return;_dfs(k,htp); for(int i=head[x];i;i=nxt[i]) if(lst[i]!=k&&lst[i]!=fa[x]) _dfs(lst[i],lst[i]);}int Lca(int x,int y){ while(top[x]!=top[y]) { if(deep[top[x]]
1&&deep[f]