zoukankan      html  css  js  c++  java
  • P3565 [POI2014]HOT-Hotels

    题目描述

    There are nn towns in Byteotia, connected with only n-1n−1 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.

    输入输出格式

    输入格式:

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

    The towns are numbered from 11 to nn.

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

    Each line contains two integers aa and bb (1le ale ble n1≤a≤b≤n) , 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


    给定一棵树,求有多少三点间距离两两相等


    (asuldb)怒嘲这道题和给定一棵树求有多少点一样简单

    其实三个点间距离两两相等当且仅当这三个点到同一个点距离相等且路径不重合,
    我们处理出到距离每个点每个距离的点的个数,然后排列组合
    每次换根,那么三个点一等在不同子树,所有情况-三个点在同一子树情况-两个点在同一子树情况即可


    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #define LL long long
    #define max(a,b) ((a)>(b)? (a):(b))
    #define min(a,b) ((a)<(b)? (a):(b))
    
    using namespace std;
    
    int dp[5011][5011],i,j,k,m,n,d[5011],ver[10011],nex[10011],head[10111],cnt,de[10011],x,y;
    LL ans;
    
    void add(int x,int y)
    {
        cnt+=1;
        ver[cnt]=y; nex[cnt]=head[x]; head[x]=cnt;
    }
    
    void dfs1(int now,int fa,int f)
    {
        int r[10001];
        for(int i=1;i<=f;i++) r[i]=d[i];
        
        for(int i=1;i<=max(f,de[now]);i++) 
        {
            if(d[i]>=3) ans-=(LL)d[i]*(d[i]-1)*(d[i]-2)/6;
        
            dp[now][i]+=d[i];
            if(dp[now][i]>=3) ans+=(LL)dp[now][i]*(dp[now][i]-1)*(dp[now][i]-2)/6;
        }
        
        for(int i=head[now];i;i=nex[i])
        {
            int t=ver[i];
            if(t==fa) continue;
            for(int j=1;j<=de[t];j++) 
            {
                if(dp[t][j]>=3) ans-=(LL)dp[t][j]*(dp[t][j]-1)*(dp[t][j]-2)/6;
                if(dp[now][j+1]>=3 && dp[t][j]>=2) ans-=(LL)dp[t][j]*(dp[t][j]-1)*(dp[now][j+1]-dp[t][j])/2;
            }
        }
        
        for(int i=1;i<=max(f,de[now]);i++) 
            if(d[i]>=2 && dp[now][i]>=3) ans-=(LL)d[i]*(d[i]-1)*(dp[now][i]-d[i])/2;
        
        for(int i=head[now];i;i=nex[i])
        {
            int t=ver[i];
            int g=max(f+1,de[now]+1);
            if(t==fa) continue;
            for(int j=g;j>=2;j--) 
                d[j]=dp[now][j-1]-dp[t][j-2];
            d[1]=1;
            dfs1(t,now,g);
            for(int j=1;j<=f;j++) d[j]=r[j];
        }
            
    }
    
    void dfs(int now,int fa)
    {
        de[now]=1;
        dp[now][0]=1;
        for(int i=head[now];i;i=nex[i])
        {
            int t=ver[i];
            if(t==fa) continue;
            dfs(t,now);
            de[now]=max(de[now],de[t]+1);
            for(int j=0;j<=de[t];j++) dp[now][j+1]+=dp[t][j];
        }
    }
    
    int main()
    {
        scanf("%d",&n);
        for(i=1;i<n;i++) 
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        dfs(1,0);
        dfs1(1,0,1);
        printf("%lld",ans);
    }
    
  • 相关阅读:
    iStylePDF c#集成开发示例
    纯js 实现 HTML 元素拖拽,
    前端自动滚动
    双向选择排序(暂定)
    uniapp 分享链接
    Could not find a declaration file for module 'vue-xxx'.
    精通JavaScript(重点内容笔记)更新中...
    如何让DIV模块随着页面固定和不固定
    序列不包含任何匹配元素
    PHPStorm配置Apache服务器(wampServer 版)
  • 原文地址:https://www.cnblogs.com/ZUTTER/p/9814856.html
Copyright © 2011-2022 走看看