3611: [Heoi2014]大工程|树形DP|虚树

构建出虚树然后DP统计答案
自己写的DP太傻QAQ,各种WA
膜了一发PoPoQQQ大爷的DP方法
mxdis,mndis 分别表示到当前点最近和最远的被选出来的点的距离
mx,mn 分别表示在以当前点为根的情况下距离最远的两点的距离和距离最近的两点的距离。
sum 表示在以当前点为根的子树中,所有关键的到当前点的距离之和
c <script type="math/tex" id="MathJax-Element-224">c</script>数组表示以当前点为根的子树中,关键点的个数
然后我用了个时间戳来标记关键点

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define inf 0x7FFFFFFF
#define ll long long
#define N 1000005
using 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]]<deep[top[y]])swap(x,y);
        x=fa[top[x]];
    }
    return deep[x]<deep[y]?x:y;
}
bool cmp(int x,int y){return S[x]<S[y];}
ll dis(int x,int y)
{
    return deep[x]+deep[y]-2*deep[Lca(x,y)];
}
void dp(int x)
{
    c[x]=tim[x]==TI;sum[x]=0;
    mx_dis[x]=tim[x]==TI?0:-inf;
    mn_dis[x]=tim[x]==TI?0:inf;
    mn[x]=inf;mx[x]=-inf;
    for(int i=Head[x];i;i=Nxt[i])
    {
        ll d=dis(Lst[i],x); dp(Lst[i]);
        ans+=sum[x]*c[Lst[i]]+(sum[Lst[i]]+c[Lst[i]]*d)*c[x];
        c[x]+=c[Lst[i]];
        sum[x]+=sum[Lst[i]]+c[Lst[i]]*d;
        mx[x]=max(mx[x],max(mx[Lst[i]],mx_dis[Lst[i]]+d+mx_dis[x]));
        mn[x]=min(mn[x],min(mn[Lst[i]],mn_dis[Lst[i]]+d+mn_dis[x]));
        mx_dis[x]=max(mx_dis[x],mx_dis[Lst[i]]+d);
        mn_dis[x]=min(mn_dis[x],mn_dis[Lst[i]]+d);
    }
    Head[x]=0;
}

void solve()
{
    int n=sc(),top=0;Tot=0;TI++;
    for(int i=1;i<=n;i++) 
        tim[a[i]=sc()]=TI;
    sort(a+1,a+n+1,cmp);
    if(a[1]!=1)st[++top]=1;
    for(int i=1;i<=n;i++)
    {
        int t=a[i],f=0;
        while(top)
        {
            f=Lca(st[top],t);
            if(top>1&&deep[f]<deep[st[top-1]]) Insert(st[top-1],st[top]),top--;
            else if(deep[f]<deep[st[top]]){Insert(f,st[top]);top--;break;}
            else break;
        }
        if(st[top]!=f&&f)st[++top]=f;
        st[++top]=t;
    }
    while(--top)Insert(st[top],st[top+1]);
    ans=0;dp(1);
    printf("%lld %lld %lld\n",ans,mn[1],mx[1]);
}   
int main()
{
    n=sc();
    for(int i=1;i<n;i++)
    {
        int x=sc(),y=sc();
        insert(x,y);
    }
    dfs(1,0);_dfs(1,1);
    for(int i=1,m=sc();i<=m;i++)
        solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值