Tree
Time Limit: 1000MS   Memory Limit: 30000K
     

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

Source

树分治模板题;

分治算法在树的路径问题中的应用

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e4+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct is
{
int v,w,nex;
}edge[N<<];
int head[N],edg;
void add(int u,int v,int w)
{
edge[++edg]=(is){v,w,head[u]};head[u]=edg;
} int son[N],msi[N],d[N];
int vis[N],deep[N];
int n,K,ans,root,sum;
void groot(int u,int fa)
{
son[u]=,msi[u]=;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(v==fa||vis[v])continue;
groot(v,u);
son[u]+=son[v];
msi[u]=max(msi[u],son[v]);
}
msi[u]=max(msi[u],sum-son[u]);
if(msi[u]<msi[root])root=u;
}
void gdeep(int x,int fa)
{
deep[++deep[]]=d[x];
for(int i=head[x];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(v==fa||vis[v])continue;
d[v]=d[x]+w;
gdeep(v,x);
}
} int rootans(int x,int base)
{
d[x]=base;deep[]=;
gdeep(x,);
sort(deep+,deep++deep[]);
int ans=,l=,r=deep[];
while(l<r)
{
if(deep[l]+deep[r]<=K)
{
ans+=r-l;
l++;
}
else r--;
}
return ans;
}
void dfs(int u)
{
ans+=rootans(u,);
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(vis[v])continue;
ans-=rootans(v,w);
root=;sum=son[v];
groot(v,u);
dfs(root);
}
}
void init(int n)
{
memset(head,-,sizeof(head));
memset(msi,,sizeof(msi));
memset(son,,sizeof(son));
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
memset(deep,,sizeof(deep));
sum=n;root=;edg=;
msi[]=inf;
ans=;
}
int main()
{
while(~scanf("%d%d",&n,&K))
{
if(n==&&K==)break;
init(n);
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);
}
groot(,);
dfs(root);
printf("%d\n",ans);
}
return ;
}

poj 1744 tree 树分治的更多相关文章

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

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

  2. POJ 1741 Tree 树分治

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

  3. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  4. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  5. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  6. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  7. POJ 1741 Tree ——点分治

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

  8. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  9. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

随机推荐

  1. VIM编码检查

    trouble shooting https://www.django.cn/article/show-4.html https://blog.csdn.net/lh756437907/article ...

  2. 您的windows许可证即将过期 win10的解决办法

    出现这个错误是因为安装的版本不是正版系统,每隔一段时间需要激活 这次激活也费了一些时间,记录如下希望能对大家有所帮助 (1)首先可以查看自己的许可什么什么时候会过期 windows+R调出命令运行窗口 ...

  3. python的__str__()和__repr__()方法

    __str__()和__repe__()方法定义一个值通过print打印时返回时的显示样式 l=list('hello') print(l) #['h', 'e', 'l', 'l', 'o'] cl ...

  4. OSI七层协议与TCP/IP模型

    OSI为Open System Interconnection的缩写,意为开放式系统互联,国际标准化组织(ISO,International Organization for Standardizat ...

  5. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  6. ssh 工具

    ssh 使用 rsa key 实现无密码访问 server A 要用 rsa key 验证的方式访问 server B 在A上创建/root/.ssh/目录 > chmod -R 700 /ro ...

  7. 叶亚明:合格CTO的六要素(转)

    叶亚明,携程旅行网CTO & 高级技术副总裁,负责携程的移动.Online.呼叫中心等的技术架构.开发及运营.在加入携程之前,叶亚明是ebay.com技术平台总监,领导ebay.com几代网站 ...

  8. HTML <frame> 标签的 src 属性

    HTML <frame> 标签 实例 src 属性规定在框架中显示的文档的位置: <html> <frameset cols="50%,50%"> ...

  9. 05文件合并脚本--By Mark Lutz

    ''' 合并split.py创建的目录下的所有组分文件以重建文件. 依赖文件名的排序:长度必须一致. ''' import os,sys readsize=1024 def join(fromdir, ...

  10. Python3 tkinter基础 Canvas create_rectangle 画虚边的矩形 create_oval 画椭圆形 圆形

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...