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

    题目描述
    There are towns in Byteotia, connected with only 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 (), the number of towns in Byteotia.
    The towns are numbered from to .
    The Byteotian road network is then described in lines.
    Each line contains two integers and () , separated by a single space, that indicate there is a direct road between the towns and .
    In tests worth of the total point number an additional condition ![](h…
    输出格式:
    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个点。两两距离相等。有多少种方案?
    。
    有没有瞬间觉得题目变水了。。
    

    思路

    大佬这题O(n)过,本蒟蒻表示只会O(n^2)的方法。
    首先这三个点一定不形成一条链,一定存在不是这三个点的一个点使这个点到这三个点的距离相等。所以我们直接枚举n个点,Dfs周围每个子树,记录深度,然后乘法原理。

    常数巨大的丑陋代码

    # include <stdio.h>
    # include <stdlib.h>
    # include <iostream>
    # include <string.h>
    # include <math.h>
    using namespace std;
    
    # define IL inline
    # define RG register
    # define UN unsigned
    # define ll long long
    # define rep(i, a, b) for(RG int i = a; i <= b; i++)
    # define per(i, a, b) for(RG int i = b; i >= a; i--)
    # define uev(e, u) for(RG int e = ft[u]; e != -1; e = edge[e].nt)
    # define mem(a, b) memset(a, b, sizeof(a))
    # define max(a, b) ((a) > (b)) ? (a) : (b)
    # define min(a, b) ((a) < (b)) ? (a) : (b)
    
    IL int Get(){
        RG char c = '!'; RG int num = 0, z = 1;
        while(c != '-' && (c > '9' || c < '0')) c = getchar();
        if(c == '-') z = -1, c = getchar();
        while(c >= '0' && c <= '9') num = num * 10 + c - '0', c = getchar();
        return num * z;
    }
    
    const int MAXN = 5001, INF = 2147483647;
    struct Edge{
        int to, nt;
    } edge[MAXN << 1];
    int n, ft[MAXN], cnt, out[MAXN], tot[MAXN], deep, t1[MAXN], t2[MAXN];
    ll ans;
    
    IL void Dfs(RG int u, RG int fa, RG int d){
        deep = max(deep, d); tot[d]++;
        uev(e, u){
            RG int v = edge[e].to;
            if(v == fa) continue;
            Dfs(v, u, d + 1);
        }
    }
    
    int main(){
        mem(ft, -1);
        n = Get();
        rep(i, 1, n - 1){
            RG int u = Get(), v = Get();
            edge[cnt] = (Edge){v, ft[u]}; ft[u] = cnt++;
            edge[cnt] = (Edge){u, ft[v]}; ft[v] = cnt++;
            out[u]++; out[v]++;
        }
        rep(i, 1, n){
            if(out[i] < 3) continue;
            mem(t1, 0); mem(t2, 0); deep = 1;
            uev(e, i){
                RG int v = edge[e].to;
                Dfs(v, i, 1);
                rep(j, 1, deep){
                    ans += t1[j] * tot[j];
                    t1[j] += tot[j] * t2[j];
                    t2[j] += tot[j];
                    tot[j] = 0;
                }
            }
        }
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    华为全联接大会2019,共创智能新高度
    CTDC2019首席技术官领袖峰会,AI赋能 智享5G
    2019全球体验设计峰会:体验赋能商业,创造更好体验
    全球闪存峰会旨在深化技术创新,增进闪存产业链上下游
    PyCon 2019火热来袭,与大数据、人工智能等专家一起探讨Python语言
    PHPConChina 2019 PHP开发者大会将于8月在上海举办!
    2019腾讯Live开发者大会(TLC),引领技术新趋势
    2019 HTML5深度应用开发实践
    2019年5G物联网关键技术与应用培训,了解5G网络发展现状及进展
    2019第二届企业云服务大会 -- 企业智变,云化未来
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206422.html
Copyright © 2011-2022 走看看