POJ 1741 Tree (点分治)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 20816 | Accepted: 6820 |
Description
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
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
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
Source
给一棵边带权树,问两点之间的距离小于等于K的点对有多少个。
将无根树转化成有根树进行观察。满足条件的点对有两种情况:两个点的路径横跨树根,两个点位于同一颗子树中。
如果我们已经知道了此时所有点到根的距离a[i],a[x] + a[y] <= k的(x, y)对数就是结果,这个可以通过排序之后O(n)的复杂度求出。然后根据分治的思想,分别对所有的儿子求一遍即可,但是这会出现重复的——当前情况下两个点位于一颗子树中,那么应该将其减掉(显然这两个点是满足题意的,为什么减掉呢?因为在对子树进行求解的时候,会重新计算)。
在进行分治时,为了避免树退化成一条链而导致时间复杂度变为O(N^2),每次都找树的重心,这样,所有的子树规模就会变的很小了。时间复杂度O(Nlog^2N)。
树的重心的算法可以线性求解。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+;
const int M=1e6+;
int n,m,k,tot,size,root,ans;
int head[N],s[N],f[N],d[N];
bool done[N];
vector<int>dep;
struct man{
int to,next,l;
}edg[N*];
void add(int u,int v,int l){
edg[tot].to=v;edg[tot].l=l;edg[tot].next=head[u];head[u]=tot++;
}
void getroot(int u,int fa){
s[u]=;f[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=fa&&!done[v]){
getroot(v,u);
s[u]+=s[v];
f[u]=max(f[u],s[v]);
}
}
f[u]=max(f[u],size-s[u]);
if(f[u]<f[root])root=u;
}
void getdep(int u,int fa){
dep.push_back(d[u]);
s[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=fa&&!done[v]){
d[v]=d[u]+edg[i].l;
getdep(v,u);
s[u]+=s[v];
}
}
}
int calc(int u,int init){
dep.clear();
d[u]=init;
getdep(u,);
sort(dep.begin(),dep.end());
int ret=;
for(int l=,r=dep.size()-;l<r; ){
if(dep[l]+dep[r]<=k)ret+=r-l++;
else r--;
}
return ret;
}
void work(int u){
ans+=calc(u,);
done[u]=true;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(!done[v]){
ans-=calc(v,edg[i].l);
f[]=size=s[v];
getroot(v,root=);
work(root);
}
}
}
int main(){
while(~scanf("%d%d",&n,&k)&&n&&k){
tot=ans=;f[]=size=n;
met(head,-);met(done,false);
int u,v,l;
for(int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&l);
add(u,v,l);add(v,u,l);
}
getroot(,root=);
work(root);
printf("%d\n",ans);
}
return ;
}
POJ 1741 Tree (点分治)的更多相关文章
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- [poj 1741]Tree 点分治
题意 求树上距离不超过k的点对数,边权<=1000 题解 点分治. 点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
随机推荐
- fis难用的地方
1. 刷新不同步,刷新的结果是前一次的修改结果2. 刷新时间非常长3. 有些代码打包不兼容,例如tween这个库,有函数yoyo:function yoyo(yoyo){}的形式,不能正确打包,会报[ ...
- spring 配置文件读取 mysql username报错
在配置项目中,spring读取jdbc.properties文件连接mysql时报错: java.sql.SQLException: Access denied for user 'Admini ...
- 对Office文档进行授权
Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationCl ...
- pip 使用国内源
常用国内的pip源如下:阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple ...
- Linux下rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- 《vue.js实战》练习---数字输入框组件
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- AOP编程的常用实现方式
aop代理分为静态代理.jdk动态代理.cglib动态代理 通过动态代理的方式实现横向扩展,实现权限校验.日志等功能. jdk静态代理:代理类和委托类实现同一接口,并且在代理类中需要硬编码接口. jd ...
- Sublime Text 3 遇到的一些小坑的解决方法
1.[不停弹出更新框]Sublime Text 3 软件会弹出“Update Available”对话框,点击“Cancel”按钮取消:取消之后还是会频繁出现 解决方法:点击菜单栏“Preferenc ...
- python-列表 字典 集合 元祖 字符串的相关总结练习
1.执行python脚本的两种方式指定解释器执行在交互器中执行 2.简述位.字节的关系:ASCII1个二进制位是计算机里的最小表示单元1个字节是计算机里最小的储存单元二进制位=8bits(位)8bit ...
- shell命令行混合进制计算器smartbc
需要简单的计算的时候,不想用GUI的计算器,能在shell下直接计算就最好了 查了下,有个东西叫 bc, 具体的使用就不赘述了,可以运行bc,然后进去计算,也可以echo传递过去,大概是像这样 ec ...