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

    题目描述

    There are nnn towns in Byteotia, connected with only n−1n-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 nnn (1≤n≤5 0001le nle 5 0001n5 000), the number of towns in Byteotia.

    The towns are numbered from 111 to nnn.

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

    Each line contains two integers aaa and bbb (1≤a≤b≤n1le ale ble n1abn) , separated by a single space, that indicate there is a direct road between the towns aaa and bbb.

    输出格式:

    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个点。两两距离相等。有多少种方案?

    本题数据很小,n^2的可以过

    先枚举转折点,在dfs找到深度,f1[i]表示有1个点距离为i的个数,f2[i]表示有2个点距离为i的个数,ans累加

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    
    const int maxn = 5e3+10;
    
    int n,size,head[maxn],maxd;
    
    ll f1[maxn],f2[maxn],sum[maxn];
    
    struct edge{
        int v,nex;
    }e[maxn<<1];
    
    void adde(int u,int v) {
        e[size].v=v;e[size].nex=head[u];head[u]=size++;
    }
    
    void dfs(int u,int fa,int dis) {
        sum[dis]++;maxd=max(maxd,dis);
        for(int i=head[u];~i;i=e[i].nex ){
            int v=e[i].v;
            if(v==fa) continue;
            dfs(v,u,dis+1);
        }
    }
    
    int main() {
        memset(head,-1,sizeof(head));
        scanf("%d",&n);
        for(int i=1;i<n;i++) {
            int u,v;scanf("%d%d",&u,&v);
            adde(u,v);adde(v,u);
        }
        ll ans=0;
        for(int u=1;u<=n;u++) {
            memset(f1,0,sizeof(f1));
            memset(f2,0,sizeof(f2));
            for(int i=head[u];~i;i=e[i].nex) {
                int v=e[i].v;
                maxd=1;
                dfs(v,u,1);
                for(int j=1;j<=maxd;j++) {
                    ans+=f2[j]*sum[j];f2[j]+=f1[j]*sum[j];f1[j]+=sum[j];sum[j]=0;
                }
            }
        }
        printf("%lld",ans);
        return 0;
        
    } 
    View Code

    据说可以优化,但我不知道

  • 相关阅读:
    加入页面切换动画, 避免冷启动
    Spark Shuffle模块——Suffle Read过程分析
    java使用线程请求訪问每次间隔10分钟连续5次,之后停止请求
    二叉排序树(BST)构造与应用
    L贪心基础
    leetcode笔记:Ugly Number II
    Android API Guides---Layouts
    红外目标图像中阈值切割方法的比較与研究
    IOS开发证书变成“此证书的签发者无效”解决方法
    Web前端开发规范收集
  • 原文地址:https://www.cnblogs.com/plysc/p/10919106.html
Copyright © 2011-2022 走看看