Description

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
题解
求树上总共有多少点对(u,v)距离<=k
题意
初学点分治
树的重心定义:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡
点分治就是把树按子树重心不断DFS,然后处理每个子树对答案的贡献,会用到容斥,把n^2的复杂度优化为nlogn
处理每个子树对答案的贡献,先求出重心到各个点的距离,然后sort一下,然后可以二分出满足<=k的答案
代码
 #include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=1e4+; vector< pair<int,int> >G[maxn];
int mx[maxn],size[maxn],vis[maxn],dis[maxn],ans,MIN,n,k,num,root;
void dfssize(int u,int fa)
{
size[u]=;
mx[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
if(!vis[v]&&v!=fa)
{
dfssize(v,u);
size[u]+=size[v];
mx[u]=max(mx[u],size[v]);
}
}
}
void dfsroot(int r,int u,int fa)//r为子树的根
{
if(size[r]-size[u]>mx[u])//子树的其余节点
mx[u]=size[r]-size[u];
if(mx[u]<MIN)//root为重心
MIN=mx[u],root=u;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
if(!vis[v]&&v!=fa)
dfsroot(r,v,u);
}
}
void dfsdis(int u,int fa,int d)
{
dis[num++]=d;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
int w=G[u][i].second;
if(!vis[v]&&v!=fa)
dfsdis(v,u,d+w);
}
}
int cal(int r,int w)
{
int ret=;
num=;
dfsdis(r,r,w);
sort(dis,dis+num);
int L=,R=num-;
while(L<R)
{
while(dis[L]+dis[R]>k&&L<R)R--;
ret+=R-L;
L++;
}
return ret;
}
void dfs(int u)
{
MIN=n;
dfssize(u,u);
dfsroot(u,u,u);
int Grivate=root;
ans+=cal(Grivate,);
vis[root]=;
for(int i=;i<G[Grivate].size();i++)
{
int v=G[Grivate][i].first;
int w=G[Grivate][i].second;
if(!vis[v])
{
ans-=cal(v,w);
dfs(v);
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF,n||k)
{
ans=;
for(int i=;i<=n;i++)
{
G[i].clear();
vis[i]=;
}
for(int i=,u,v,w;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
G[u].push_back({v,w});
G[v].push_back({u,w});
}
dfs();
printf("%d\n",ans);
}
return ;
}

POJ 1741 Tree(点分治点对<=k)的更多相关文章

  1. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  2. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  3. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  4. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  5. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

  6. [poj 1741]Tree 点分治

    题意 求树上距离不超过k的点对数,边权<=1000 题解     点分治.     点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...

  7. POJ - 1741 - Tree - 点分治 模板

    POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...

  8. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  9. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

随机推荐

  1. go-json处理的问题

    1.通过Decoder来解析json串 package main import ( "encoding/json" "fmt" "io" & ...

  2. Centos6.5安装mariadb的坑坑

    最近在看Ansible,<Ansible权威指南>,然后有个地方是搭建Web应用框架,有个服务器是安装Mariadb,找到官方文档,一直弄,总是报错,换个思路,下载rpm到本地,安装,然后 ...

  3. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  4. Delphi LiveBinds组件

    Component Logo Component Name Description TBindSourceDB Is used for creating bindings to databases. ...

  5. MySQL 5.7临时表空间

    MySQL 5.7起,开始采用独立的临时表空间(和独立的undo表空间不是一回事哟),命名ibtmp1文件,初始化12M,且默认无上限. 选项 innodb_temp_data_file_path 可 ...

  6. C#编程经验-enum and struct

    enum,store fixed values,use array replace,not use this data-structurestruct,store several variables, ...

  7. IIS6.0+win2003部署MVC网站的一些问题

    安装iis,framework环境不谈.MVC网站部署 步骤: 1.为程序新建一个应用程序池(将default的那个程序池作为模板就可以了) 2.web服务扩展一些启用一些必要的服务 3.新建网站 描 ...

  8. [UE4]Invalidation Box

    Invalidation Box:使条目无效的容器.使容器内的条目不再更新,如果确定某一个UI不需要更新的话,就可以把这个UI放到Invalidation Box中. 一.Invalidation B ...

  9. Python-用户登录三次错误锁定

    黑名单:blacklist.txt 用户名单:username_password.py # Author:Stephen Yuan # 用户名和密码 username_password = { ', ...

  10. Vue 目录结构 绑定数据 绑定属性 循环渲染数据

    一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...