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
树的点分治 参考了漆子超的论文 以及多位大佬的博客

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<stack>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const int N=+;
int son[N];
int ans;
int num;
int p;
int head[N];
int vis[N];
int minn;
int root;
int k;
int dis[N];
void init(){
num=;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
struct node{
int to,next,w;
}edge[N];
void add(int u,int v,int w){
edge[num].to=v;
edge[num].w=w;
edge[num].next=head[u];
head[u]=num++;
}
void getroot(int x,int y,int n){
son[x]=;
int maxx=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==y||vis[v])continue;
getroot(v,x,n);
son[x]=son[x]+son[v];
maxx=max(maxx,son[v]);
}
maxx=max(maxx,n-son[x]);
//cout<<maxx<<" "<<x<<endl;
if(minn>maxx){
minn=maxx;
root=x;
}
}
void getdeep(int x,int v0,int y){
dis[p++]=y;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==v0||vis[v])continue;
getdeep(v,x,y+edge[i].w);
}
}
int cal(int x,int y){
int res=;
p=;
getdeep(x,,y);
sort(dis,dis+p);
int l=;
int r=p-;
while(l<r){
if(dis[l]+dis[r]<=k){
res=res+(r-l);
l++;
}
else{
r--;
}
}
return res;
}
void work(int x){
ans=ans+cal(x,);
//cout<<cal(x,0)<<" ";
// cout<<endl;
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v]==){
ans=ans-cal(v,edge[i].w);
minn=INF;
getroot(v,,son[v]);
work(root);
}
}
}
int main(){
int n;
while(scanf("%d%d",&n,&k)!=EOF){
if(n==&&k==)break;
init();
for(int i=;i<n-;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
ans=;
minn=INF;
getroot(,-,n);
//for(int i=1;i<=n;i++)cout<<son[i]<<" ";
// cout<<endl;
//cout<<root<<" "<<minn<<endl;
work(root);
cout<<ans<<endl;
}
return ;
}

poj 1741(树的点分治)的更多相关文章

  1. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  2. POJ 1741 树的点分治

    题目大意: 树上找到有多少条路径的边权值和>=k 这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度 #include <cstdio> #include & ...

  3. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  4. poj 1741 树的分治

    思路:这题我是看 漆子超<分治算法在树的路径问题中的应用>写的. 附代码: #include<iostream> #include<cstring> #includ ...

  5. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  6. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

  7. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  8. POJ 1741 树上 点的 分治

    题意就是求树上距离小于等于K的点对有多少个 n2的算法肯定不行,因为1W个点 这就需要分治.可以看09年漆子超的论文 本题用到的是关于点的分治. 一个重要的问题是,为了防止退化,所以每次都要找到树的重 ...

  9. POJ 1741 树上的点分治

    题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...

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

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

随机推荐

  1. mysql下载和安装Windows服务

    一.下载mysql:https://dev.mysql.com/downloads/mysql/,解压拷贝到D:\software\mysql-8.0.13-winx64 二.在D:\software ...

  2. 使用whIle循环语句和变量打印九九乘法表

    -设置i变量declare @i int --设置j变量declare @j int --设置乘法表变量declare @chengfabiao varchar(1000)--给i,j,@chengf ...

  3. Linux Shell 小知识

    ${} ——变量替换 通常 $var 与 ${var} 没有区别,但是用 ${} 会比较精确的界定变量名称的范围. name='Ace' echo "result1: my name is ...

  4. JS监听事件错误:Uncaught TypeError: xx(函数名)is not a function at HTMLInputElement.onclick

    事件监听一直出错,提示已定义的函数名不是一个函数,折腾了好久才想到,原来是函数名和JS内部关键字重名造成的. 以前也遇到过这种情况,但因为发生的概率比较小,就没太在意,但是这次感觉这方面确实需要注意, ...

  5. js 不能用关键字 delete 做函数名

    把delete更改为mydelete正常.

  6. windows程序设为开机自启动

    在Windows文件管理器中输入 %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup 把程序快捷方式放到此处即可.

  7. Unity如何播放带有alpha 通道的视频

    问题: 当使用Video Player播放带有alpha 通道的视频时带有黑色背景 解决方式: 使用文件格式为WEBM的视频,对视频文件进行的修改 在RawImage中,将New Render Tex ...

  8. CentOS / RHEL 7 : Chrony V/s NTP (Differences Between ntpd and chronyd)

    CentOS / RHEL 7 : Chrony V/s NTP (Differences Between ntpd and chronyd) Chosing between Chrony and N ...

  9. 关于 BaseHTTPServer 的介绍

    简介: (1) 基础的web服务器是一个模板,其其角色是客户端和服务器端完成必要的HTTP交互,在basehttpserver模块中可以找到一个名字叫HTTPServer 的服务器基本类 (2)处理程 ...

  10. tipsText表单验证(注册)

    注册表单验证脚本 <script src="/assets/skins/js/jquery-1.11.2.min.js"></script> <scr ...