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://poj.org/problem?id=1987 题意:给一棵树,求树上有多少对节点满足距离<=K 思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉自己加自己的方案.而且,我们还要去掉走到同一个子树的方案.复杂度:O(nlog^2n) #include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algor…
  转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 上场CF的C题是一个树的分治... 今天刚好又看到一题,就做了下 题意:一棵树,问两个点的距离<=k的点对数目. http://poj.org/problem?id=1987 貌似是经典的点分治题..... 看成有根树,那么这样的点对路径分为两种,1.过根节点,2.存在于某一棵子树当中. 显然情况2可以看成是一种子情况 对于1的统计,统计所有节…
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path…
BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治 Description     在得知了自己农场的完整地图后(地图形式如前三题所述),约翰又有了新的问题.他提供 一个整数K(1≤K≤109),希望你输出有多少对农场之间的距离是不超过K的. Input     第1到I+M行:与前三题相同:     第M+2行:一个整数K. Output       农场之间的距离不超过K的对数. Sample Inp…
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b),求a,b两点到lca(a,b)的边权之和为多少. 倍增维护树上前缀和,求得LCA之后,相应做差即可. 代码总览 #include <cstdio> #include <algorithm> #include <cstring> #include <cmath>…
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求近期公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int max…
题目大意:(同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 <<…
http://poj.org/problem?id=1987 (题目链接) 题意 给出一棵树,求树上距离不超过K的点对个数. Solution 点分治,同poj1741. 代码 // poj1987 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define LL…
Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifesty…
普通dfs访问每个点对的复杂度是O(n^2)的,显然会超时. 考虑访问到当前子树的根节点时,统计所有经过根的点(u, v)满足: dist(u) + dist(v) <= maxd,并且 belong(u)≠belong(v)(即u,v不在同一子树). 这里说的距离指的是节点到跟的距离. 可以用作差法,即用所有满足条件的点对数减去那些在根节点为当前子树根节点的儿子节点的点对数. 上面一步可以用O(nlogn)的复杂度解决,即先排序再比较. 根节点子树可以递归解决,用树的点分治. 总复杂度上界是O…
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists…
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this prob…
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 16648   Accepted: 5817 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much t…
计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解: #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #define rep(i,a,b) for(int i=a;i<=b;i++) #define clr(a,…
Distance on Chessboard Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23096   Accepted: 7912 Description 国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间.如下图所示: 王.后.车.象的走子规则如下: 王:横.直.斜都可以走,但每步限走一格. 后:横.直.斜都可以走,每步格数不受限制. 车:横.竖均可以走,不能斜走,格数不限. 象:只能斜走,格数不限.…
1.链接地址: http://bailian.openjudge.cn/practice/1657 http://poj.org/problem?id=1657 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间.如下图所示:王.后.车.象的走子规则如下: 王:横.直.斜都可以走,但每步限走一格. 后:横.直.斜都可以走,每步格数不受限制. 车:横.竖均可以走,不能斜走,格数不限. 象:只能斜走,格数不限. 写一…
Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in…
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面的那个字母无视掉,没用的.接着k,下面k个询问lca,输出即可 &题解: 首先看的这个 http://www.cnblogs.com/JVxie/p/4854719.html 大致懂了方法,之后又找了这个代码 http://blog.csdn.net/lianai911/article/details…
Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of t…
题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input…
Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14392   Accepted: 5066 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifesty…
这题和poj 1741是一模一样的 但是1741能AC的代码,在这里却是TLE,暂时没看出哪里出现了问题.. AC代码: #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <stack&g…
这道题是一道点分治的题目,难度不大,可以拿来练手. 关键是对于找出来的重心的删除操作需要删掉这条边,这很重要. 还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz. 然后就A了... 每次点分治 分两种情况讨论一下就可以啦! /w\... #include <cstdio> #include <algorithm> using namespace std; const int maxn = 56789; const int INF = 1000000007;…
Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献,这样就得到子树间的贡献了,然后再搞子树就可以,这就是一个子问题了. Code /************************************************************** Problem: 3365 User: BeiYu Language: C++ Result…
给出一棵树,对于每一个询问,给出2个节点,输出2个节点的距离. 输入中有字母,那个是没有用的,不用管. 思路: 0.选择编号为1的节点作为树的root (注意:有些题的边是单向的,这时候我们要根据节点的入度来确定root, 双向的话一般可以随意选择一个节点作为root) 1.dfs1,求出dep和pa[i][0] 2.初始化数组pa 3.节点(u,v)的权值为w 把本来是边的权值w赋给u,v中dep较大的节点, cost[i]表示节点i的权值为cost[i] 先初始化:cost[root]=0…
本题目输入格式同1984,这里的数据范围坑死我了!!!1984上的题目说边数m的范围40000,因为双向边,我开了80000+的大小,却RE.后来果断尝试下开了400000的大小,AC.题意:给出n个点,m条边. 接下来m行,每行对应u,v,len,字符(字符表示v位于u的哪个方向,在本题没有丝毫用处) 表示u和v的路径长度为len.注意本题是双向边! 给出k个查询,让你求两点间的距离.思路:可以这样处理:先处理出每个节点i到根的距离dist[i]. 设根节点1到a的路径和1到b的路径的最后一个…
好像是模板题  当作练习题 不错:  要求任意两点之间的距离.可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先:  距离就变成了 dis[u]+dis[v] - 2*dis[ lca(u,v) ]  非常好的题目 #include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #include<cmath> using namespace…
[题意] 求树上长度不超过k的点对数目. [思路] 和 Tree 一样一样的. 就是最后统计的时候别忘把根加上. [代码] #include<set> #include<cmath> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define…
思路:1741的A1送 1. #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<cmath> #define Maxn 40010 #define Maxm 80010 #define inf 0x7fffffff using namespace std; int head[Maxn],vi[Maxn],e,ans,num,k,n,…