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 ...
随机推荐
- 一些关于Spring的随笔
Spring的IOC.AOP IOC(Inversion of Control): spring容器控制了所有的bean,不用spring以前,一个bean要依赖另一个bean就在这个bean里初始化 ...
- 【学习笔记】彻底理解JS中的this
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- requirejs&&springboot
1.Spring Boot Spring boot 基础结构主要有三个文件夹: (1)src/main/java 程序开发以及主程序入口 (2)src/main/resources 配置文件 (3) ...
- git设置log的别名 for hist
hist -- alias for 'log --color --graph --date=short --pretty=format:'%Cred%h%Creset -%C(yellow)%d%C ...
- NYOJ 题目42 一笔画问题
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...
- 云态(YunCloud)的Centos服务器修改dns教程
DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终 ...
- ListView中含有EditText时候--要命的焦点问题迎刃而解
最近做项目的时候遇到了一个问题,就是在ListView的item上面含有一个EditText,要求是这样: 1当点击item的时候,item可以点击; 2当点击EditText的时候EditText也 ...
- gocode安装不上的解决办法
mkdir -p $GOPATH/src/golang.org/x //路径下创建此文件 cd $GOPATH/src/golang.org/x //切换到此目录 git clone ht ...
- QT+信号和槽函数_自定义槽函数_一个信号对应多个槽函数
以下的代码里面有自定义槽函数的内容,同时也有信号实现的函数: #ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QWidget> #i ...
- python之str (字符型)
用途: 存储少量的数据,+ *int 切片, 其他操作方法 切片还是对其进行任何操作,获取的内容全部是strl类型 存储数据单一 格式: 在python中用引号引起来的就是字符串 '今天吃了没?' 1 ...