码迷,mamicode.com
首页 > 其他好文 > 详细

[POI2014]HOT-Hotels

时间:2017-09-12 23:11:03      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:nes   有一个   div   with   ++   more   algo   via   a*   

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

标签:nes   有一个   div   with   ++   more   algo   via   a*   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/7512508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!