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 ...
随机推荐
- HashMap、ConcurrentHashMap以及HashTable(面试向)
---->HashMap 在java1.7中,hashmap的数据结构是基于数组+链表的结构,即我们比较熟悉的Entry数组,其包含的(key-value)键值对的形式.在多线程环境下,Hash ...
- IDEA基本使用及配置(2)
IDEA配置:File >> Setiings进入配置界面 1.主题配置:默认两种主题,黑色.白色,可以自己在网上下载,然后File >> Import Setiings导入, ...
- 如何系统学习并且掌握JavaScript
- crm项目之stark组件前戏(二)
stark组件的设计主要来源于django中admin的功能,在django admin中只需要将模型表进行注册,就可以在页面对该表进行curd的动作,那么django admin是如何做的呢? 在d ...
- 简单的python代码实现语音朗读
昨天女友生日,因为她一直对生日无感,所以我也就没有准备什么礼物.想起元旦前写的自动测试的脚本,添加了语音来提示测试和报告错误.灵机一动,为什么不用这个语音来庆祝她生日快乐呢?身为设计公司市场经理的她对 ...
- ZOJ 3905 Cake
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- 《C语言程序设计(第四版)》阅读心得(二)
第六章引用数组处理批量数据 1.数组的定义 :类型符 数组名[常量表达式] 1) 在主函数中定义数组,常量表达式不能包含变量 +]; //合法 int n; int a[n]; //不合法 2)在被 ...
- [HAOI2011]Problem b 题解
题目大意: 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y)=k. 思路: 设f(k)为当1≤x≤n,1≤y≤m,且n≤m,使gcd(x,y)=k的数对 ...
- Jquery判断某个字符串是否在数组中
使用$.inArray方法判断,如果存在则返回0,不存在返回-1,结果如下: 另外也可以将数组转为字符串,并使用正则表达式处理
- 2.5 3-way quickSort
1.排序时,数组含有大量重复元素,应该使用哪种排序手段? (1)mergeSort:与数组的特征无关,比较次数总是在1/2NlgN~NlgN之间 (2)quickSort:当所有的元素全都相同的时候, ...