[POI2014]HOT-Hotels

Posted 日拱一卒 功不唐捐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POI2014]HOT-Hotels相关的知识,希望对你有一定的参考价值。

https://www.luogu.org/problem/show?pid=3565

 

枚举中间点

枚举中间点的子树

枚举距离

如果只有3个子树,那么对答案的贡献为a*b*c

假设现在来了第4个子树,那么答案会增加 d*(a*b+a*c+b*c)

再来第5个,答案增加e*(a*b+a*c+a*d+b*c+b*d+c*d)

括号里的数好像有点儿规律

用c2维护来维护它

 d*(a*b+a*c+b*c)变到 e*(a*b+a*c+a*d+b*c+b*d+c*d)

增加了 e*(a+b+c)

所以用再用c1维护 前缀和

 

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5001
using namespace std;
int front[N],nxt[N<<1],to[N<<1],tot;
int dep[N],c1[N];
int maxd;
long long ans,c2[N];
void add(int u,int v)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
}
void dfs(int x,int f,int d)
{
    maxd=max(maxd,d);
    dep[d]++;
    for(int i=front[x];i;i=nxt[i])
        if(to[i]!=f) dfs(to[i],x,d+1);
}
int main()
{
    int n,u,v;
    scanf("%d",&n);
    for(int i=1;i<n;i++) scanf("%d%d",&u,&v),add(u,v);
    for(int i=1;i<=n;i++)
    {
        for(int j=front[i];j;j=nxt[j])
        {
            maxd=1;
            dfs(to[j],i,1);
            for(int k=1;k<=maxd;k++)
            {
                ans+=dep[k]*c2[k];
                c2[k]+=c1[k]*dep[k];
                c1[k]+=dep[k];
                dep[k]=0;
            }
        }
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
    }
    printf("%lld",ans);
}

 

题目描述

There are nn towns in Byteotia, connected with only n-1n1 roads.

Each road directly links two towns.

All the roads have the same length and are two way.

It is known that every town can be reached from every other town via a route consisting of one or more (direct-link) roads.

In other words, the road network forms a tree.

Byteasar, the king of Byteotia, wants three luxury hotels erected to attract tourists from all over the world.

The king desires that the hotels be in different towns and at the same distance one from each other.

Help the king out by writing a program that determines the number of possible locations of the hotel triplet in Byteotia.

有一个树形结构,每条边的长度相同,任意两个节点可以相互到达。选3个点。两两距离相等。有多少种方案?

输入输出格式

输入格式:

 

The first line of the standard input contains a single integer nn (1\le n\le 5\ 0001n5 000), the number of towns in Byteotia.

The towns are numbered from 11 to nn.

The Byteotian road network is then described in n-1n1 lines.

Each line contains two integers aa and bb (1\le a\le b\le n1abn) , separated by a single space, that indicate there is a direct road between the towns aa and bb.

 

输出格式:

 

The first and only line of the standard output should contain a single integer equal to the number of possible placements of the hotels.

 

输入输出样例

输入样例#1:
7
1 2
5 7
2 5
2 3
5 6
4 5
输出样例#1:
5

说明

有一个树形结构,每条边的长度相同,任意两个节点可以相互到达。选3个点。两两距离相等。有多少种方案?

以上是关于[POI2014]HOT-Hotels的主要内容,如果未能解决你的问题,请参考以下文章

[POI2014]HOT-Hotels

POI题目乱选

[POI 2014] Little Bird

缺少 SQL SERVER 2014 代码片段

[bzoj3872][Poi2014]Ant colony_树形dp

bzoj3524:[Poi2014]Couriers