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

大致题意:让你找出一颗树中有多少点对,满足它们之间的最短距离小于K

思路:点分治模板

代码如下

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e4+5;
struct Edge//链式向前星存边
{
    int to,cost;
    int next;
}edge[maxn*2];
int tol,head[maxn];
int vis[maxn];
int size[maxn];//子树的大小 
int maxv[maxn];//最大孩子节点的size 
int dis[maxn];
int ans,root,MAX;

int N,K;
int num;
void init()
{
    tol=0;
    ans=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
}
void add(int u,int v,int w)
{
    edge[tol].to=v;
    edge[tol].cost=w;
    edge[tol].next=head[u];
    head[u]=tol++;
}
//处理子树大小 
void dfs_size(int u,int f)
{
    size[u]=1;
    maxv[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to==f||vis[to]) continue;
        dfs_size(to,u);
        size[u]+=size[to];
        maxv[u]=max(maxv[u],size[to]);  
    } 
}

//找重心 
void dfs_root(int r,int u,int f)
{

    maxv[u]=max(maxv[u],size[r]-size[u]);//size[r]-size[u]是u上面部分的树的尺寸,跟u的最大孩子比,找到最大孩子的最小差值节点
    if(maxv[u]<MAX) 
    {
        MAX=maxv[u];
        root=u;
    }

    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to==f||vis[to]) continue;
        dfs_root(r,to,u);
    }

}
//求每个点离重心的距离 
void dfs_dis(int u,int d,int f)
{
    dis[num++]=d;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to==f||vis[to]) continue;

        dfs_dis(to,d+edge[i].cost,u);
     } 
}
//计算以u为根的子树中有多少点对的距离小于等于K
int calc(int u,int d)
{
    int ret=0;
    num=0;
    dfs_dis(u,d,0);
    sort(dis,dis+num);
    int i=0,j=num-1;
    while(i<j)
    {
        while(dis[i]+dis[j]>K&&i<j)
        j--;
        ret+=j-i;
        i++;
    }
    return ret;
}
void dfs(int u)
{
    MAX=N;
    dfs_size(u,0);
    dfs_root(u,u,0);
    ans+=calc(root,0);
    vis[root]=1;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(!vis[to])
        {
            ans-=calc(to,edge[i].cost);
            dfs(to);
        }
    }
}

int main()  
{  
    while(scanf("%d%d",&N,&K)!=EOF&&N&&K)
    {
        int u,v,w;
        init(); 
        for(int i=1;i<N;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        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、付费专栏及课程。

余额充值