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. html5——颜色

    CSS2 1.opacity,可以设置透明度,但是父盒子设置了透明度会影响子盒子 CC3 1.transparent属性,但是不可改变透明值 2.rgba():r--red g--green b--b ...

  2. jdbc 使用谨记

    jdbc是java操作数据库的杀手锏.所有java程序员,对jdbc应该都不陌生. 但是,应该你也曾经被其折磨的抓耳挠腮,咬牙切齿吧,也许正因为这样你才对其记忆犹新,刻骨铭心. 这里有一些使用jdbc ...

  3. SQL Server建库-建表-建约束

    ----------------------------------------SQL Server建库-建表-建约束创建School数据库------------------------------ ...

  4. Java笔记整理列表

    整理Java相关知识点. 2018-11-20 1:Java入门学习 2:Java进阶

  5. HDU_Reward_拓扑排序

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. python笔记之发送邮件

    发送邮件前提:开启邮箱授权码 一.开启授权码(以163邮箱为例) 1.登录163邮箱,点击设置--POP3/SMTP/IMAP,出现设置界面   2. 开启SMTP服务且可以查询SMTP的host地址 ...

  7. jquery操作元素之间相邻的元素的获取方式

    <!DOCTYPE html><html> <head> <style> .siblings * { display: block; border: 2 ...

  8. php第十六节课

    分页 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $ ...

  9. Linux自动化之基于http的pxe安装服务

    PXE:     Preboot Excution Environment 预启动执行环境     Intel公司研发     基于Client/Server的网络模式,支持远程主机通过网络从远端服务 ...

  10. PAT 1091. Acute Stroke (bfs)

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...