POJ1751 Tree 树分治
分析:每次找重心可以发现最多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 树分治的更多相关文章
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- HDU4871 Shortest-path tree(树分治)
好久没做过树分治的题了,对上一次做是在南京赛里跪了一道很裸的树分治题后学的一道,多校的时候没有看这道题,哪怕看了感觉也看不出来是树分治,看出题人给了解题报告里写了树分治就做一下好了. 题意其实就是给你 ...
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
- HDU 4812 D Tree 树分治
题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...
- 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 ...
随机推荐
- 使用枚举(emum)代替常量类
原文: 作者:逍遥不羁 来源:CSDN 原文:https://blog.csdn.net/javaloveiphone/article/details/52371706 版权声明:本文为博主原创文章, ...
- (蓝桥杯)2018JAVA B组 日志分析
日志统计 小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是: ts id 表示在ts时刻编号id的帖子收到一个"赞". ...
- stark组件之时间插件(九)
在模型model中用的都是时间字段DateTimeField字段,在后台处理中可以看到,在生成modelform过程中,继承的是BaseModelForm,而其对时间字段加入了特殊的date_time ...
- url方法使用与单例模式
一.url方法使用 from django.contrib import admin from django.urls import path, include from django.conf.ur ...
- vs2003 刷新项目失败。无法从服务器中检索文件夹信息
环境: 操作系统:windows server 2003 开发工具:Visual stuadio 2003 FrameWork: 1.1 打开web项目的时候报错 提示 项目刷新失败,无法从服务器 ...
- (dede)织梦系统二次开发笔记
(dede)织梦系统二次开发记录 --soulsjie 一.模板常用文件说明 模板文件都在文件夹templets下,我们以默认模板(default)为例,对模板文件结构进行分析: 首页模板文件目录 \ ...
- POJ1094 / ZOJ1060
#include <cstdio> #include <cstring> #include <stack> #include <iostream> us ...
- bzoj 1962 硬币游戏 (猜数问题)
[bzoj1962]模型王子 2015年3月26日1,6460 Description Input 输入数据共一行,两个整数N,K,用一个空格隔开,具体意义如题目中所述. Output 输出数据共一行 ...
- 关于android系统启动不同activity默认过渡动画不同的一些认识
在同一个android设备里,发现不同的app启动时显示的过渡动画是不同的.查看显示不同过渡动画的两个app的源码,其设置的主题都是同一个主题,但是为什么过渡动画效果不同呢?后来发现,activity ...
- 【BZOJ2142】礼物(扩展lucas定理,中国剩余定理合并方程)
题意:有n件礼物,m个人,每个人分别需要w[i]件礼物,求分礼物的不同方案数 mod P 提示:设P=p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct,pi为质数. 1≤n≤10^ ...