POJ 1987 Distance Statistics 树分治】的更多相关文章

Distance Statistics     Description Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <…
  转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 上场CF的C题是一个树的分治... 今天刚好又看到一题,就做了下 题意:一棵树,问两个点的距离<=k的点对数目. http://poj.org/problem?id=1987 貌似是经典的点分治题..... 看成有根树,那么这样的点对路径分为两种,1.过根节点,2.存在于某一棵子树当中. 显然情况2可以看成是一种子情况 对于1的统计,统计所有节…
http://poj.org/problem?id=1987 题意:给一棵树,求树上有多少对节点满足距离<=K 思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉自己加自己的方案.而且,我们还要去掉走到同一个子树的方案.复杂度:O(nlog^2n) #include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algor…
第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重,求有多少对顶点之间的边的权重之和小于等于K. 分析: 树分治. 直接枚举不可,我们将树划分成若干子树. 那么两个顶点有两种情况: u,v属于同一子树的顶点对 u,v属于不同子树的顶点对 第一种情况,对子树递归即可求得. 第二种情况,从u到v的路径必然经过了顶点s,只要先求出每个顶点到s的距离再做统…
思路参考于: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> #incl…
题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 40010 #define INF 0x3f3f3f3f using namespace std; int points,edges,k; int head[MAX],total; int next[MAX <<…
Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8554   Accepted: 2545 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 i…
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define inf 0x7fffffff ; struct node{ int to,ne…
这道题是一道点分治的题目,难度不大,可以拿来练手. 关键是对于找出来的重心的删除操作需要删掉这条边,这很重要. 还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz. 然后就A了... 每次点分治 分两种情况讨论一下就可以啦! /w\... #include <cstdio> #include <algorithm> using namespace std; const int maxn = 56789; const int INF = 1000000007;…
Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 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…