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 ...
随机推荐
- 如何解决MySQL在高版本需要指明是否进行SSL连接问题
WARN: Establishing SSL connection without server's identity verification is not recommended. Accordi ...
- es5/6数组遍历以及常用的一些方法
数组的遍历方法 1...for(var i=0;i<arr.length;i++){ } ---------------------------------------------------- ...
- RPC(Remote Procedure Call Protocol)远程过程调用协议
RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...
- 【Conclusion】MySQL的安装和使用
MySQL使用 因为数据库实验用到了MySQL,这里对现在已经涉及到的MySQL部分操作做一个简单的小结. 1.安装MySQL 上MySQL的官网下载对应自己OS平台的MySQL安装文件,有在线安装和 ...
- (译文)IOS block编程指南 4 声明和创建blocks
Declaring and Creating Blocks (声明和创建blocks) Declaring a Block Reference (声明一个block引用) Block variable ...
- (转)编码剖析Spring管理Bean的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...
- linux下C的建立、编译和运行 gcc (附上Windows下visual c++的用法)
2019/6/24 1. 环境:window10下安装了MobaXterm,这里申请了阿里云的服务账号,可以直接使用linux系统,避免安装虚拟机等. 2. 判断linux下是否有GCC编译工具(我们 ...
- C-基础:C语言为什么不做数组下标越界检查
//这段代码运行有可能不报错.]; ;i<;i++) { a[i]=i; } 1.为了提高运行效率,不检查数组下表越界,程序就可以跑得快.因为C语言并不是一个快速开发语言,它要求开发人员保证所有 ...
- JAVA中等待所有线程都执行结束(转2)
场景: package com.java4all.mypoint; import java.util.concurrent.CountDownLatch; public class ThreadTes ...
- 第1节 flume:4、离线项目处理的整个架构图;5、flume的基本介绍;
第1节 flume:4.离线项目处理的整个架构图 辅助系统工具:flume,azkaban,sqoop. 在一个完整的离线大数据处理系统中,除了hdfs+mapreduce+hive组成分析系统的核心 ...