分析:每次找重心可以发现最多n层,每层复杂度是O(nlogn) 总体时间复杂度是O(nlog^2n)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int N=1e4+;
int head[N],tot,n,k,ret,cnt,sz[N],rt,p,dis[N],mx[N];
bool vis[N];
struct Edge{
int v,w,next;
}edge[N<<];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void getsz(int u,int f){
sz[u]=;mx[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;if(v==f||vis[v])continue;
getsz(v,u);sz[u]+=sz[v];
if(sz[v]>mx[u])mx[u]=sz[v];
}
}
void getroot(int tp,int u,int f){
mx[u]=max(mx[u],sz[tp]-sz[u]);
if(mx[u]<cnt)cnt=mx[u],rt=u;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;if(v==f||vis[v])continue;
getroot(tp,v,u);
}
}
void getdis(int u,int f,int d){
dis[++p]=d;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;if(v==f||vis[v])continue;
getdis(v,u,d+edge[i].w);
}
}
int cal(int u,int d){
int ans=;
p=;
getdis(u,-,d);
sort(dis+,dis++p);
for(int i=,j=p;i<j;++i){
while(dis[i]+dis[j]>k&&i<j)--j;
ans+=j-i;
}
return ans;
}
void dfs(int u){
cnt=n;getsz(u,-);getroot(u,u,-);
ret+=cal(rt,);vis[rt]=true;
for(int i=head[rt];~i;i=edge[i].next){
int v=edge[i].v;if(vis[v])continue;
ret-=cal(v,edge[i].w);
dfs(v);
} }
int main(){
while(~scanf("%d%d",&n,&k),n&&k){
for(int i=;i<=n;++i)head[i]=-,vis[i]=false;
ret=tot=;
for(int i=;i<n;++i){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
dfs();
printf("%d\n",ret);
}
return ;
}

POJ1751 Tree 树分治的更多相关文章

  1. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  2. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  3. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  4. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  5. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  6. HDU4871 Shortest-path tree(树分治)

    好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...

  7. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  8. HDU 4812 D Tree 树分治

    题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...

  9. CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT

    Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...

随机推荐

  1. <Redis> 入门五 持久化RBD/AOF

    RDB RDB持久化是指在指定的时间间隔内将内存中的数据集快照写入磁盘(默认是 dump.rdb). 默认持久化机制,就是将内存中的数据以快照的方式写入二进制文件dump.rbd中. 触发快照的条件 ...

  2. 树莓派 - gpio-led platform driver 控制LED

    树莓派3b板上有两个LED, pwr (power) 和 act (activity).是platform_driver gpio-led驱动. 可以通过设备树和gpio-led来额外控制一个LED. ...

  3. python链家网高并发异步爬虫and异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  4. Python中关于使用正则表达式相关的部分笔记

    一点点自己记的笔记,如果各位朋友看不懂,可以在评论区留言,会尽可能快的回复. 所有的知识点全部贴在代码上了,注释也写了. 建议大伙把代码拷到自己的机器上,运行,查看结果,然后,结合注释,再自己稍稍理解 ...

  5. 【转】jquery 注册事件的方法

    原文链接:http://outofmemory.cn/code-snippet/2123/jquery-zhuce-event-method 1.使用事件名来绑定,可用的事件名有 change,cli ...

  6. html的href标签不能下载apk文件

    解决方案: 打开Internet 服务管理器Internet 服务管理器 网站属性 HTTP头(MIME类型) 新建 扩展名:.apk 类型(MIME): application/vnd.androi ...

  7. Django用法补充

    1. 自定义Admin from django.contrib import admin from xx import models # 自定义操作 class CustomerAdmin(admin ...

  8. JS基础:函数

    函数声明和函数表达式 在 JS 中定义函数的方式有两种:一种是函数声明,一种是函数表达式. 例如: //函数声明 function fun() { ... } //函数表达式 var f = func ...

  9. 文件权限设置与http,php的关系

    在web服务器上的文件要使用什么权限比较好呢.我开始的时候直接都是777,后台安全部门的同事,通过漏洞把我管理的服务器给搞了.报告到我这里,我才意识到权限的设置不能马虎.环境采用nginx+php,一 ...

  10. codevs1154 能量项链

    题目描述 Description 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子 ...