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. [Android]异常1-duplicate files during packaging of

    异常原因: 可能一>项目存在重复资源文件 可能二>Android Studio与源代码Android Studio不一致 解决方法有: 解决一>查找重复资源,删除或者修改文件 解决二 ...

  2. cmd 切换目录和配置环境变量

    记录一下: 在用cmd进行切换盘符的时候, 如果是从 C盘切换到其他盘的话: D:直接回车就行了. 如果是在同一个盘符内切换文件夹的话,cd D:\ComputerSoft\curl\curl-7.6 ...

  3. 170925_2 Python socket 创建UDP的服务器端和客户端

    [python版本]3.6 UDP服务器端: from socket import * from time import ctime host = '' port = 21567 buf_size = ...

  4. sublime之markdown快捷键

    目录 sublime 快捷键 markdown技能 sublime 快捷键 ctrl + shift + p 进入命令面板 package install 进入下载仓库 ctrl + alt + O ...

  5. codeforces_738C_二分

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. 文件上传原理--FileReader

    单个文件:<div> <input value="上传" type="file" id="photos_upload"&g ...

  7. CAD在图纸保存的同时,也把基本信息保存了(网页版)

    主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgToURLEx 保存DWG文件到服务器上的扩展函数.详细说明如下: 参数 说明 pszServerUrl 服务器网 ...

  8. 从 UI 交互角度说语音识别产品

    语言是人类进化的主要特征,而人工智能拥有了说话的能力也是科技进步的一个特征.在很多科幻的电影里面,我们可以看到人工智能的身影.在电影 her 里面见到的人工智能,真的让人叹为观止,他可以随意的和你聊天 ...

  9. Python 发送邮件、加密 day5

    一.发送邮件import yagmail username = 'xxxxx@126.com'#发邮件人使用的邮箱 password = '123abc' #免费的邮箱,这里用授权码,一般自己公司的, ...

  10. springboot 使用idea打包 遇到问题

    找了很久错误后来发现添加这三句话就可以了 需要在项目的pom.xml文件中加上第47-49行的3句话