zoukankan      html  css  js  c++  java
  • [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 (1le nle 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 (1le ale ble 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个点。两两距离相等。有多少种方案?

  • 相关阅读:
    对称加密和非对称加密
    数字签名
    内存溢出和内存泄漏
    生产随机字符串
    天才高中生参与斯坦福新研究:在图像压缩上,人类比算法强
    用机器人替代宇航员!日本打造远程操作机器人
    机器人也能拥有“物理直觉”?谷歌这款机器人真是厉害了
    35所高校新设人工智能本科专业 专家:人工智能非孤立专业
    “入职1年,我和做 AI 的同学薪资翻了 2 翻!”
    让机器学会认脸究竟有多少用处
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7512508.html
Copyright © 2011-2022 走看看