【bzoj3522】[Poi2014]Hotel

Description

有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达。吉丽要给他的三个妹子各开(一个)房(间)。三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同。
有多少种方案能让吉丽满意?

Input

第一行一个数n。
接下来n-1行,每行两个数x,y,表示x和y之间有一条边相连。

Output

让吉丽满意的方案数。

Sample Input

7

1 2

5 7

2 5

2 3

5 6

4 5

Sample Output

5

HINT

【样例解释】

{1,3,5},{2,4,6},{2,4,7},{2,6,7},{4,6,7}

【数据范围】

n≤5000

题解
枚举三个点中心开始DP

代码(by hzwer)

#include<bits/stdc++.h>
#define inf 1000000000
#define ll long long 
using namespace std;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,mx,cnt;
int last[5005],deep[5005],tmp[5005];
ll s1[5005],s2[5005],ans;
struct edge{
    int to,next;
}e[10005];
void insert(int u,int v)
{
    e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
}
void dfs(int x,int fa)
{
    mx=max(deep[x],mx);
    tmp[deep[x]]++;
    for(int i=last[x];i;i=e[i].next)
        if(e[i].to!=fa)
        {
            deep[e[i].to]=deep[x]+1;
            dfs(e[i].to,x);
        }
}
int main()
{
    n=read();
    for(int i=1;i<n;i++)
    {
        int u=read(),v=read();
        insert(u,v);
    }
    for(int x=1;x<=n;x++)
    {
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        for(int i=last[x];i;i=e[i].next)
        {
            deep[e[i].to]=1;
            dfs(e[i].to,x);
            for(int j=1;j<=mx;j++)
            {
                ans+=s2[j]*tmp[j];
                s2[j]+=tmp[j]*s1[j];
                s1[j]+=tmp[j];
            }
            for(int j=1;j<=mx;j++)
                tmp[j]=0;
        }
    }
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值