tree(poj 1741)
题意:给一颗n个节点的树,每条边上有一个距离v(v<=1000)。定义d(u,v)为u到v的最小距离。给定k值,求有多少点对(u,v)使u到v的距离小于等于k。
/*
照着点分治模板敲了敲,有很多细节地方应该注意,不然很容易出错。
点分治的大概就是解决树上路径中统计某些东西的一个算法,顾名思义,肯定与递归有些关系。
简单步骤就是:①求重心 ②统计这棵树的答案 ③递归到它的子树求解
推荐一篇博客:http://www.cnblogs.com/chty/p/5912360.html
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 10010
#define inf 100000000
using namespace std;
int head[N],deep[N],d[N],vis[N],son[N],f[N],root,sum,ans,n,m;
struct node{
int to,t,pre;
};node e[N*];
void add(int i,int x,int y,int z){
e[i].to=y;
e[i].t=z;
e[i].pre=head[x];
head[x]=i;
}
void getroot(int x,int fa){
son[x]=;f[x]=;
for(int i=head[x];i;i=e[i].pre){
if(vis[e[i].to]||e[i].to==fa) continue;
getroot(e[i].to,x);
son[x]+=son[e[i].to];
f[x]=max(f[x],son[e[i].to]);
}
f[x]=max(f[x],sum-son[x]);
if(f[x]<f[root])root=x;
}
void getdeep(int x,int fa){//求出x的子树上的节点的深度(x深度不一定为0)
deep[++deep[]]=d[x];
for(int i=head[x];i;i=e[i].pre){
if(vis[e[i].to]||e[i].to==fa) continue;
d[e[i].to]=d[x]+e[i].t;
getdeep(e[i].to,x);
}
}
int cal(int x,int v){//求出x的子树上的答案,但是对于本题目多计算了两个点在同一条子树上的情况
d[x]=v;deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int l=,r=deep[],tot=;
while(l<r){
if(deep[r]+deep[l]<=m) tot+=r-l,l++;
else r--;
}
return tot;
}
void solve(int x){//求出x的子树上的答案
ans+=cal(x,);
vis[x]=;
for(int i=head[x];i;i=e[i].pre){
if(vis[e[i].to]) continue;
ans-=cal(e[i].to,e[i].t);
sum=son[e[i].to];
root=;
getroot(e[i].to,);
solve(root);
}
}
int main(){
while(scanf("%d%d",&n,&m)){
if(!n&&!m) break;
ans=root=;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(i*-,x,y,z);add(i*,y,x,z);
}
sum=n;f[]=inf;
getroot(,);
solve(root);
printf("%d\n",ans);
}
return ;
}
tree(poj 1741)的更多相关文章
- 【BZOJ2588】Count On a Tree(主席树)
[BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...
- hdu 4670 Cube number on a tree(点分治)
Cube number on a tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- 【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- 【POJ1741】Tree(点分治)
[POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...
随机推荐
- ES6学习笔记(4)----正则的扩展
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 正则的扩展 ES6新增的正则表达式修饰符 u修饰符a.能够更准确地匹配unicode大于\uFF ...
- 类成员的指针必须NULL化,否则是乱七八糟的东西
class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...
- (2)Ngixn 编译安装设置开机自启
设置nginx开机自启 #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 ...
- python实现: protobuf解释器
之前项目为了自动化,所以写一个protobuf的解释器,用来生成项目所需的格式. 当然现在通过以下链接的指导,跳过手工分析,直接生成代码了. https://developers.google.com ...
- 光线步进——RayMarching入门
入门实现 先用RayMarching描绘一个球体,最后在进行光照计算参考:https://www.shadertoy.com/view/llt3R4 模拟摄像机射线float3 rayDirectio ...
- Logisim的使用
准备 通过Logisim的官网下载适合你机器的Logisim的软件,启动Logisim应用程序(Logisim可能有点bug,如果程序运行诡异,可能内部已经奔溃,最好的解决方法是重新启动它). Log ...
- 细说PHP-5.3.4变量的引用赋值
变量总是传值赋值.也就是说,当讲一个表达式的值赋予一个变量时,整个原始表达式的值被赋值到目标变量.这意味着,当一个变量的值赋予另个一变量时,改变其中一个变量的值,将不会影响到另一个变量.PHP中提供了 ...
- chrome 打开上次关闭的tab ctrl+shift+T
chrome 打开上次关闭的tab ctrl+shift+T
- 原生查找DOM的方法
JS获取DOM元素的方法(8种) 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTagName) 通过类名 ...
- postman使用--发送请求
概述 上节讲了下接口的基础,从现在来学习怎么测接口.当然,测试接口有很多的工具,比如postman,jmeter等等,或者用代码测试,如果是做接口自动化我当然会选python,如果是调试接口,我特别喜 ...