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 ...
随机推荐
- python工具之日志记录
''' 写日志类 日志存放目录为当前应用程序的目录下的log目录中 日志产生规则:每小时产生一个文件 write by :wujf 2017-02-24 ''' import sys import o ...
- JSP报错The value for the useBean class attribute *** is invalid.
环境:IDEA+Tomcat9+JDK1.8 在前期学习时,环境一直能够"正常"使用,实际上环境并没有完全搭建成功. 推荐: https://blog.csdn.net/lw_po ...
- String.format()【示例详解】
String.format()[示例详解] 整理者:Vashon 前言: String.format 作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 String.fo ...
- xcopy递归拷贝
递归拷贝 ::xcopy SOURCE_DIR DES_DIR\ /s SOURCE_DIR后面不需要加反斜杠
- js单线程和js异步操作的几种方法
一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事. JavaScript的单线程,与它的用途有关.作为浏览器脚本语言,JavaS ...
- codevs 2600 13号星期几?
时间限制: 1 s 空间限制: 8000 KB 题目等级 : 黄金 Gold 题目描述 Description 从1900年1月1日(星期一) 开始经过的n年当中,每个月的13号这一天是星期一.星 ...
- SEO 第四章
SEO第四章 课程目标: 掌握网站TDK的优化方法 1. 页面TKD介绍 Title keywords description 标题 关键字 描述 网站的每一个页面都有三大标签(主页.栏目页.内容 ...
- 云原生技术图谱 (CNCF Landscape)
转自:https://raw.githubusercontent.com/cncf/landscape/master/landscape/CloudNativeLandscape_latest.jpg
- zabbix告警邮件、短信发送错误快速排查方法
zabbix告警邮件.短信发送错误快速排查方法 背景 zabbix告警邮件.短信经常有同事反馈发送错误的情况,这个问题排查的角度很多,那么最快捷的角度是什么呢? 在我看来,最快的角度就是判断这个告警邮 ...
- 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function https://www.cnblogs.com/jaso ...