POJ 1741 Tree(点分治点对<=k)
Description
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 last test case is followed by two zeros.
Output
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
#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)的更多相关文章
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- [poj 1741]Tree 点分治
题意 求树上距离不超过k的点对数,边权<=1000 题解 点分治. 点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- Centos6.5安装mariadb的坑坑
最近在看Ansible,<Ansible权威指南>,然后有个地方是搭建Web应用框架,有个服务器是安装Mariadb,找到官方文档,一直弄,总是报错,换个思路,下载rpm到本地,安装,然后 ...
- leetcode习题练习
day001 #!user/bin/env python # -*- coding:utf-8 -*- #day001 两数之和 #方法1 def Sum(nbs,tgt): len_nums = l ...
- mysql 计算两点经纬度之间的距离含具体sql语句
mysql距离计算,单位m,以及排序 lon 经度 lat 纬度 一般地图上显示的坐标顺序为,纬度在前(范围-90~90),经度在后(范围-180~180) 首先新建一张表,里面包含经纬度 SET F ...
- minIni: A minimal INI file parser
https://www.compuphase.com/minini.htm https://github.com/compuphase/minIni
- docker内存监控与压测
一直运行的docker容器显示内存已经耗尽,并且容器内存耗尽也没出现重启情况,通过后台查看发现进程没有占用多少内存.内存的监控使用的是cadvisor,计算方式也是使用cadvisor的页面计算方式, ...
- Java执行JavaScript代码
Java执行JavaScript代码 这篇文章主要为大家详细介绍了Java执行JavaScript代码的具体操作方法,感兴趣的小伙伴们可以参考一下 我们要在Java中执行JavaScriptMetho ...
- 01-Introspector内省机制
在java领域编程中,内省机制相当的不错,可以省去我们程序员很多的不必要的代码 比如说:在jdbc工具类 我们可以将ResultSet结果集待到 javabean对象中 将http请求报文的数据 转换 ...
- WordPress版微信小程序开发系列(一):WordPress REST API
自动我发布开源程序WordPress版微信小程序以来,很多WordPress站长在搭建微信小程序的过程中会碰到各种问题来咨询我,有些问题其实很简单,只要仔细看看我写的文章,就可以自己解决.不过这些文章 ...
- import 搜索路径
来源 http://www.runoob.com/python/python-mysql.html 搜索路径 当你导入一个模块,Python 解析器对模块位置的搜索顺序是: 1.当前目录 2.如果不在 ...
- Redis之父九条编程忠告
最近在学习redis,特地了解了一下redis之父Salvatore Sanfilippo ,而看到了一篇优秀的文章,总解分享之 个人解读总结如下 取巧编程品质key word: 过硬的编码能力 快 ...