#POJ1987#Distance Statistics(树+点分治)

13 篇文章 0 订阅
4 篇文章 0 订阅

Distance Statistics
Time Limit: 2000MS Memory Limit: 64000K
Total Submissions: 2472 Accepted: 883
Case Time Limit: 1000MS

Description

Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K <= 1,000,000,000) and wants to know how many pairs of farms lie at a distance at most K from each other (distance is measured in terms of the length of road required to travel from one farm to another). Please only count pairs of distinct farms (i.e. do not count pairs such as (farm #5, farm #5) in your answer). 

Input

* Lines 1 ..M+1: Same input format as in "Navigation Nightmare" 

* Line M+2: A single integer, K. 

Output

* Line 1: The number of pairs of farms that are at a distance of at most K from each-other. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
10

Sample Output

5

Hint

There are 5 roads with length smaller or equal than 10, namely 1-4 (3), 4-7 (2), 1-7 (5), 3-5 (7) and 3-6 (9). 

这个题是在做分治题的时候看到的,所以很自然就认为是分治了

在想完思路以后觉得不难想,但是就是并不太会写

然后写了很久才写出来,还跑去参考了别人的代码(因为我是真蠢)

实现的时候注意要首先选择重心,这样才不会导致树失衡退化。


具体思路请参考漆子超大神2009集训队论文:https://wenku.baidu.com/view/1bc2e4ea172ded630b1cb602.html

里面的第一道题就是这个,写得很详细呐。


Code:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

const int MaxN = 50005;

struct node{
    int v, w, nxt;
}edge[MaxN << 1];

int N, M, K, cnt;
int Ans = 0, tot, Max_Size, Root;
int fir[MaxN], Sz[MaxN], Mu[MaxN], Dis[MaxN];
bool vis[MaxN];

bool getint(int & num){
    char c; int flg = 1;    num = 0;
    while((c = getchar()) < '0' || c > '9'){
        if(c == '-')    flg = -1;
        if(c == -1) return 0;
    }
    while(c >= '0' && c <= '9'){
        num = num * 10 + c - 48;
        if((c = getchar()) == -1)   return 0;
    }
    num *= flg;
    return 1;
}

void addedge(int a, int b, int c){
    edge[++ cnt].v = b, edge[cnt].w = c, edge[cnt].nxt = fir[a], fir[a] = cnt;
    edge[++ cnt].v = a, edge[cnt].w = c, edge[cnt].nxt = fir[b] ,fir[b] = cnt;
}

void Get_Size(int u, int fa){
    Sz[u] = 1;
    Mu[u] = 0;
    for(int i = fir[u]; i; i = edge[i].nxt) if(! vis[edge[i].v] && edge[i].v != fa){
        Get_Size(edge[i].v, u);
        Sz[u] += Sz[edge[i].v];
        Mu[u] = max(Mu[u], Sz[edge[i].v]);
    }
}

void Get_Root(int R, int u, int fa){
    Mu[u] = max(Sz[R] - Sz[u], Mu[u]);
    if(Mu[u] < Max_Size)    Max_Size = Mu[u], Root = u;
    for(int i = fir[u]; i; i = edge[i].nxt) if(! vis[edge[i].v] && edge[i].v != fa)
        Get_Root(R, edge[i].v, u);
}

void Get_Dis(int u, int d, int fa){
    Dis[++ tot] = d;
    for(int i = fir[u]; i; i = edge[i].nxt) if(! vis[edge[i].v] && edge[i].v != fa)
        Get_Dis(edge[i].v, d + edge[i].w, u);
}

int Count(int u, int d){
    int rt = 0;
    tot = 0;
    Get_Dis(u, d, 0);
    sort(Dis + 1, Dis + tot + 1);
    int i = 1, j = tot;
    while(i < j){
        while(Dis[i] + Dis[j] > K && i < j) --j;
        rt += j - i;
        ++ i;
    }
    return rt;
}

void Dfs(int u){
    Max_Size = N;
    Get_Size(u, 0);//求子树大小
    Get_Root(u, u, 0);//求重心
    Ans += Count(Root, 0);//所有满足的条件的(包括重复)
    vis[Root] = 1;
    //printf("%d\n", Root);
    for(int i = fir[Root]; i; i = edge[i].nxt)  if(! vis[edge[i].v]){
        Ans -= Count(edge[i].v, edge[i].w);//去除重复
        Dfs(edge[i].v);
    }
}

int main(){
    char s[5];
    while(getint(N) && getint(M)){
        Ans = 0, cnt = 0;
        memset(fir, 0, sizeof(fir));
        memset(vis, 0, sizeof(vis));
        int a, b, c;
        for(int i = 1; i <= M; ++ i){
            getint(a), getint(b), getint(c);
            scanf("%s", s);
            addedge(a, b, c);
        }
        getint(K);
        Dfs(1);
        printf("%d\n", Ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值