向点分治发起进攻

A - Tree POJ - 1741

Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
点分治模板题

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 10007;
struct edge{
    int v,w;
};
bool vis[N];
vector<edge> G[N];
int n,k;
int root,son[N],ms[N],sz;
long long ret = 0;
vector<int> vp;
void getroot(int x,int f){
    ms[x] = 0;
    son[x] = 1;
    for(int i = 0;i < G[x].size();i ++){
        edge &e = G[x][i];
        if(e.v == f || vis[e.v]==false) continue;
        getroot(e.v,x);
        son[x] += son[e.v];
        ms[x] = max(ms[x],son[e.v]);
    }
    ms[x] = max(ms[x],sz-son[x]);
    if(ms[x] < ms[root]) root = x;
    //cout <<"!!!" << x << ' '<< ms[x] << ' '<<sz << endl;
}

void getdep(int x,int d,int f){
    //cout << x << ' '<<d << ' ' << f << endl;
    son[x] = 1;
    if(d <= k)vp.push_back(d);
    for(int i = 0;i < G[x].size();i ++){
        int v = G[x][i].v,w = G[x][i].w;
        if(v == f||vis[v]==false) continue;
        getdep(v,d+w,x);
        son[x] += son[v];
    }
}


void dfs(int x,int f){
    vp.clear();
    long long ans = 0;
    for(int i = 0;i < G[root].size();i ++){
        int v = G[root][i].v;
        int tmp = vp.size();
        if(v == f||vis[v] == false) continue;
        getdep(v,G[root][i].w,root);
        sort(vp.begin()+tmp,vp.end());
        int j = vp.size()-1;
        for(int t = tmp;t < vp.size();t ++){
            while(j >= tmp && vp[j]+vp[t] > k) j --;
            if(j >= t) ans -= j-tmp;
            else ans -= j-tmp+1;
        }
    }
    sort(vp.begin(),vp.end());
    int j = vp.size()-1;
    for(int i = 0;i < vp.size();i ++){
        while(j >= 0 && vp[j]+vp[i] > k) j --;
        if(j >= i) ans += j;
        else ans += j+1;
        //cout << "###" << i << ' '<<j << ' '<< vp[i]<< ' '<<vp[j] << endl;
    }
    //cout << "!!!!"<< x << ' '<<root << ' ' << tmp << ' '<< vp.size()<<endl;
    ans /= 2;
    ans += vp.size();
    ret += ans;
    int rt = root;
    vis[root] = false;
    //cout << root << ' ' << ans << endl;
    for(int i = 0;i < G[rt].size();i ++){
        int v = G[rt][i].v;
        if(v == f||vis[v]==false) continue;
        sz = son[v];
        root = 0;
        getroot(v,rt);
        dfs(v,rt);
    }


}




int main(){
    while(scanf("%d %d",&n,&k)==2){
        if(n == 0) break;
        memset(vis,true,sizeof vis);
        ret = 0;
        for(int i = 1;i <= n;i ++) G[i].clear();
        for(int i = 1;i < n;i ++) {
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            G[u].push_back({v,w});
            G[v].push_back({u,w});
        }
        root = 0;
        ms[0] = n;
        sz = n;
        getroot(1,-1);

        dfs(1,-1);
        cout << ret << endl;
    }
    return 0;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值