点分治模板-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.
题目大意就是给一棵树,每条边有一个长度,两个点之间的距离就是连接它俩的边的长度之和。求出这棵树中满足距离不大于 K 的点对个数。
1.对于一棵子树的分治,首先求出它的重心, 我们目的是对这棵子树讨论所有经过重心的路径。(重心的作用是保证时间复杂度)
(一棵子树的重心其实就是你要找到一个点,使得删掉这个点后,这棵子树剩下最大(节点个数最多)联通块最小。)
2.之后,以此重心为根节点,对这棵树进行一遍深搜,得出每个节点到重心的距离dis[]
3.之后,这棵子树中相连的路径会经过重心且对答案有贡献的点对(i,j) (i<j)就会是这样: dis[i]+dis[j] <= K 且在去除重心后,i 与 j 不在同一个联通块里。
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn=10004;

struct node
{
    int v;
    int w;
    int from;
}edge[maxn<<1];

int n,m,allnode;
int root,ans,cnt=-1;
int siz[maxn],son[maxn];//子树的大小,点的最大子树的大小
int vis[maxn],head[maxn];
int depth[maxn],dis[maxn];

void ADD(int u,int v,int w)
{
    edge[++cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].from=head[u];
    head[u]=cnt;
}

void get_root(int u,int fa)//求重心
{
    siz[u]=1;
    son[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].from)
    {
        int v=edge[i].v;
        if(vis[v]||v==fa)
        {
            continue;
        }
        get_root(v,u);
        siz[u]+=siz[v];
        son[u]=max(son[u],siz[v]);
    }
    son[u]=max(son[u],allnode-siz[u]);
    if(son[u]<son[root])
    {
        root=u;
    }
}

void get_depth(int u,int fa)//获取子树所有节点与根的距离
{
    depth[++depth[0]]=dis[u];
    for(int i=head[u];i!=-1;i=edge[i].from)
    {
        int v=edge[i].v;
        if(v==fa||vis[v])
        {
            continue;
        }
        dis[v]=dis[u]+edge[i].w;
        get_depth(v,u);
    }
}

int cal(int u,int now)//以重心为u的情况下,所有情况的结果
{
    dis[u]=now;depth[0]=0;
    get_depth(u,0);
    sort(depth+1,depth+depth[0]+1);
    int res=0;
    int l=1,r=depth[0];
    while(l<r)
    {
        if(depth[l]+depth[r]<=m)
        {
            res+=(r-l);
            l++;
        }
        else
        {
            r--;
        }
    }
    return res;
}

void work(int u)//以u为重心进行计算
{
    vis[u]=1;
    ans+=cal(u,0);
    for(int i=head[u];i!=-1;i=edge[i].from)
    {
        int v=edge[i].v;
        if(vis[v])
        {
            continue;
        }
        ans-=cal(v,edge[i].w);
        allnode=siz[v];
        root=0;
        get_root(v,u);
        work(root);
    }
}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
    {
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        cnt=-1;
        for(int i=1;i<n;i++)
        {
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            ADD(u,v,w);
            ADD(v,u,w);
        }
        root=ans=0;
        allnode=n;
        son[0]=0x3f3f3f3f;
        get_root(1,0);
        work(root);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2019-08-05 21:34  weilong13  阅读(164)  评论(0)    收藏  举报