• 3611: [Heoi2014]大project|树形DP|虚树


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

    #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 ",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; }

  • 相关阅读:
    Python3笔记029
    Python3笔记028
    Python3笔记027
    Python3笔记026
    Python3笔记025
    Python3笔记024
    find the oracle logs
    asm command
    网上看到的一个朋友写的不错收藏着.
    在IBM AIX上安装Oracle RAC ,很多人都在找的东东.....
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7380994.html
Copyright © 2020-2023  润新知