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;
    }
  • 相关阅读:
    EOF ---shell编程
    Linux错误代码含义
    linux连接sybase数据库-isql
    CH7-WEB开发(集成在一起)
    [转]ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)
    [转]ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)
    [转]ASP.NET MVC 入门8、ModelState与数据验证
    [转]ASP.NET MVC 入门7、Hellper与数据的提交与绑定
    [转]ASP.NET MVC 入门6、TempData
    [转]ASP.NET MVC 入门5、View与ViewData
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206422.html
Copyright © 2011-2022 走看看