思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述。

  复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排序复杂度为nlogn,而找重心的复杂度为dfs的复杂度——O(n),因此总的复杂度为O(nlognlogn)。

  因为第一次写树分治,留个代码:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = + ; struct edge
{
int v,w;
};
int n,k,root,sz,ans;
bool vis[N];
int son[N],f[N],d[N];
vector<edge> G[N];
vector<int> dep;
void init()
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++) G[i].clear();
memset(son,,sizeof son);
memset(f,,sizeof f);
}
void addEdge(int u,int v,int w)
{
G[u].push_back((edge){v,w});
G[v].push_back((edge){u,w});
}
void getRoot(int u,int fa)
{
son[u] = ; f[u] = ;
for(int i=;i<G[u].size();i++)
{
edge e = G[u][i];
int v = e.v;
if(v == fa || vis[v]) continue;
getRoot(v,u);
son[u] += son[v];
f[u] = max(f[u], son[v]);
}
f[u] = max(f[u], sz-son[u]);
if(f[u] < f[root]) root = u;
}
void getDep(int u,int fa)
{
dep.push_back(d[u]);
for(int i=;i<G[u].size();i++)
{
edge e = G[u][i];
int v = e.v, w= e.w;
if(v == fa || vis[v]) continue;
d[v] = d[u] + w;
getDep(v,u);
}
}
int cal(int u,int len)
{
dep.clear(); d[u] = len;
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 solve(int u)
{
ans += cal(u, );
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
edge e = G[u][i];
int v = e.v, w = e.w;
if(vis[v]) continue;
ans -= cal(v, w);
f[] = sz = son[v];
getRoot(v, root=);
solve(root);
}
} int main()
{
while(scanf("%d%d",&n,&k) == )
{
if(n == && k == ) break;
init();
for(int i=;i<=n-;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
}
f[] = sz = n;
getRoot(,root=);
ans = ;
solve(root);
printf("%d\n",ans);
}
return ;
}

POJ 1741 Tree ——(树分治)的更多相关文章

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

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

  2. POJ 1741 Tree 树分治

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

  3. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  4. poj 1744 tree 树分治

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

  5. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

  6. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

  7. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  8. Tree POJ - 1741【树分治】【一句话说清思路】

    因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...

  9. POJ 1741 Tree 树的分治(点分治)

    题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...

  10. POJ 1741 Tree(点分治点对<=k)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

随机推荐

  1. 关于dataset

    举个栗子: <div id="cost" data-drink="coffee" data-food="sushi" data-mea ...

  2. MySQL5.7 启动报错:initialize specified but the data directory has files in it. Aborting.

    $ vi /etc/my.cnf ## datadir=/var/lib/mysql, 这个是data保存目录,进入/var/lib/mysql后,查看到确实有数据. #解决方法:将/var/lib/ ...

  3. linux 中free命令

    1.free 命令的选项使用 free 命令查看服务器内存使用情况.free [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V](1) ...

  4. 深度学习_1_Tensorflow_1

    # 深度学习 # 图像识别,自然语言处理 # 机器学习 深度学习 # 分类:神经网络(简单) 神经网络(深度) # 回归 图像:卷积神经网络 # 自然语言处理:循环神经网络 # cpu:运行操作系统, ...

  5. 【6】Zookeeper脚本及API

    一.客户端脚本 1.1.客户端连接 cd /usr/local/services/zookeeper/zookeeper-3.4.13/bin ##连接本地Zookeeper服务器 sh zkCli. ...

  6. 如何入门Pytorch之三:如何优化神经网络

    在上一节中,我们介绍了如何使用Pytorch来搭建一个经典的分类神经网络.一般情况下,搭建完模型后训练不会一次就能达到比较好的效果,这样,就需要不断的调整和优化模型的各个部分.从而引出了本文的主旨:如 ...

  7. idou老师教你学istio2:监控能力介绍

    我们知道每个pod内都会有一个Envoy容器,其具备对流入和流出pod的流量进行管理,认证,控制的能力.Mixer则主要负责访问控制和遥测信息收集. 如拓扑图所示,当某个服务被请求时,首先会请求ist ...

  8. 实用: 将程序的内容写出到excel中

    pom <!-- 读取excel文件 --><dependency> <groupId>org.apache.poi</groupId> <art ...

  9. 长期专业版 mac pycharm

    https://www.52pojie.cn/forum.php?mod=viewthread&tid=757722&tdsourcetag=s_pcqq_aiomsg

  10. java8 stream两个集体交集、差集、并集操作

    业务场景: 页面左右两个datagrid,双击左边datagrid行,移动到右边datagrid,右边datagrid行双击,移动到左边datagrid 点击保存,提交修改的数据到后台 后台要把查询到 ...