bzoj4543: [POI2014]Hotel加强版

一个悲伤的故事。。。

今天%你赛的t1全场都做过原题。。。

蒟蒻瑟瑟发

n^2的DP还是能够想到的

我写不下去了你还是再%一次

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;

struct node
{
    int x,y,next;
}a[2*maxn];int len,last[maxn];
void ins(int x,int y)
{
    len++;
    a[len].x=x;a[len].y=y;
    a[len].next=last[x];last[x]=len;
}
int md[maxn],son[maxn];
void pre_tree_node(int x,int fr)
{
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fr)
        {
            pre_tree_node(y,x);
            if(son[x]==0||md[son[x]]<md[y])son[x]=y;
            md[x]=max(md[x],md[y]+1);
        }
    }
}

LL space[20*maxn],*now;
LL *f[maxn],*g[maxn];
void create(int x)
{
    f[x]=now,now+=md[x]+3;
    g[x]=now+md[x]+3,now+=2*(md[x]+3);
}
LL ans;
void dfs(int x,int fr)
{
    if(son[x]!=0)
    {
        f[son[x]]=f[x]+1,g[son[x]]=g[x]-1;
        dfs(son[x],x);
    }
    f[x][0]=1;ans+=g[x][0];
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fr&&y!=son[x])
        {
            create(y);
            dfs(y,x);
            for(int j=1;j<=md[y];j++)ans+=f[x][j-1]*g[y][j];
            for(int j=0;j<=md[y];j++)ans+=g[x][j+1]*f[y][j];
            for(int j=0;j<=md[y];j++)g[x][j+1]+=f[x][j+1]*f[y][j];
            for(int j=1;j<=md[y];j++)g[x][j-1]+=g[y][j];
            for(int j=0;j<=md[y];j++)f[x][j+1]+=f[y][j];
        }
    }
}

int main()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    int n,x,y;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        ins(x,y),ins(y,x);
    }
    pre_tree_node(1,0);
    now=space+1;create(1);
    dfs(1,0);
    printf("%lld
",ans);
    
    return 0;
}
原文地址:https://www.cnblogs.com/AKCqhzdy/p/10471616.html