POJ - 1741 Tree (点分治 求路径距离小于等于k的点对)

Tree

 

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

点分治主要是处理统计树上路径这类的问题,对一个无根树,选择一点为根,然后求出所有点到根的距离,然后统计经过根的所有路径对答案的贡献,然后删掉这个点,再选另一个点为根,反复直到树上没有点。每次选择的点都是当前树的重心。具体可以见:点分治详解

题目链接:http://poj.org/problem?id=1741

题目大意:给一棵树,n个节点,n-1条边,求树上距离小于等于k的点对的数量

思路:点分治模板题

dep[i] 为点 i 到根的距离,把dep从小到大排序后,二分,如果dep[l]+dep[r]<=k,则有r-l个点对符合,累加完后,要再减去所有在同一子树下的,因为同一子树下的点对路径不经过根。

代码:
 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e4+10;
const int inf=1e9+10;
int n,k,root,ans,sum,tot,first[N];
int siz[N],f[N],vis[N],d[N],dep[N];
struct node
{
    int v,w,nex;
}e[N<<1];
void add(int u,int v,int w)
{
    e[tot].v=v,e[tot].w=w;
    e[tot].nex=first[u];
    first[u]=tot++;
}
void init()
{
    f[0]=inf;
    tot=ans=root=0;
    memset(first,-1,sizeof(first));
    memset(vis,0,sizeof(vis));
}
void getroot(int u,int fa) //求重心
{
    siz[u]=1,f[u]=0; //siz[i]记录以i为根的树的节点数,f[i]记录以i为根的最大子树的大小
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v==fa||vis[v]) continue;
        getroot(v,u);
        siz[u]+=siz[v];
        f[u]=max(f[u],siz[v]);
    }
    f[u]=max(f[u],sum-siz[u]);
    if(f[u]<f[root]) root=u; //把子树最大值最小的更新为重心,作为根
}
void getdep(int u,int fa) 
{
    dep[++dep[0]]=d[u];  //dep[i]记录点i到根的距离
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v==fa||vis[v]) continue;
        d[v]=d[u]+e[i].w;
        getdep(v,u);
    }
}
int cal(int u,int w) //计算点对数
{
    d[u]=w;
    dep[0]=0;
    getdep(u,0);
    sort(dep+1,dep+1+dep[0]);
    int l=1,r=dep[0],res=0;
    while(l<r)
    {
        if(dep[l]+dep[r]<=k)
        {
            res+=r-l;
            l++;
        }
        else r--;
    }
    return res;
}
void solve(int u)
{
    ans+=cal(u,0); 
    vis[u]=1; //删除根节点
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(!vis[v])
        {
            ans-=cal(v,e[i].w); //减去同一子树下的
            root=0,sum=siz[v];
            getroot(v,0);
            solve(root);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&k)&&(n+k))
    {
        init();
        int u,v,w;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        sum=n;
        getroot(1,0);
        solve(root);
        printf("%d\n",ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章