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;
}

 

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