poj 1741 树的点分治(入门)】的更多相关文章

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an…
题目大意: 树上找到有多少条路径的边权值和>=k 这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度 #include <cstdio> #include <cstring> #include <iostream> #include <vector> #include <algorithm> using namespace std; #define N 10005 int n , m , k , first[N];…
  Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16172   Accepted: 5272 Description    Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v.    …
题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典的树分治的题.假设我们选择了一个参考点u,那么对于不同的点对(u,v),(u , v)之间的路径有两种情况,经过点u,和不经过点u,加入我算出了没有经过点u的对数,然后把经过点u的加起来就是答案了,很简单,这就是分治的思想.具体看代码. #include<cstdio> #include<c…
思路:这题我是看 漆子超<分治算法在树的路径问题中的应用>写的. 附代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<cmath> #define Maxn 10010 #define Maxm 20010 #define inf 0x7fffffff using namespace std; int head[…
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O(n^2logn)$算法会超时, 利用树的重心进行点分治可以将$O(n^2logn)$的上界优化为近似$O(nlogn)$ 足以在1000ms的测试时间内通过 具体原理参考注释 #include<iostream> #include<map> #include<string>…
                                                                    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20816   Accepted: 6820 Description Give a tree with n vertices,each edge has a length(positive integer less than 100…
Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an…
题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k. 题解: 典型的树的分治,板子题. #include<cstdio> #include<algorithm> #include<cstring> #define F(i,a,b) for(int i=a;i<=b;++i) using namespace std; ; ],nxt[N*],w[N*],ed,ans; int…
题意就是求树上距离小于等于K的点对有多少个 n2的算法肯定不行,因为1W个点 这就需要分治.可以看09年漆子超的论文 本题用到的是关于点的分治. 一个重要的问题是,为了防止退化,所以每次都要找到树的重心然后分治下去,所谓重心,就是删掉此结点后,剩下的结点最多的树结点个数最小. 每次分治,我们首先算出重心,为了计算重心,需要进行两次dfs,第一次把以每个结点为根的子树大小求出来,第二次是从这些结点中找重心 找到重心后,需要统计所有结点到重心的距离,看其中有多少对小于等于K,这里采用的方法就是把所有…