How far away ? HDU - 2586】的更多相关文章

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27977    Accepted Submission(s): 11218 Problem Description There are n houses in the village and some bidirectional roads connectin…
题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最小的节点即为u.v的lca(最近公共祖先),RMQ把它找到.   难点: 何为dfs序: 就是树上dfs依次遍历的节点编号的序列.它的特点是回溯时会[再次]经过非叶节点,非叶节点在dfs序中出现多次,且dfs序列个数>节点个数. 为什么要用dfs序: 因为u.v的lca只出现在u.v的dfs序间.比…
HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like th…
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组 因为 \(2^{16} > 40000\) 所以所以表示祖先的数组dp[][]第二维取到16即可 就这道题来说,与比较tarjan比较,稍快一点 代码: #include <iostream> #include <algorithm> #includ…
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11320    Accepted Submission(s): 4119 Problem Description   There are n houses in the village and some bidirectional roads…
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24439    Accepted Submission(s): 9739 Problem Description There are n houses in…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; ; typedef pair<int , int>P; vector <P> G[MAXN]; ] , ok…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问两个房子a,b之间的距离是多少. 很明显的最近公共祖先问题,先建一棵树,然后求出每一点i到树根的距离dis[i],然后每次询问a,b之间的距离=dis[a]+dis[b]-2*dis[LCA(a,b)]; LCA(a,b)即是a,b的最近公共祖先.. 关于最近公共祖先,…
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的距离,然后只要计算出两点的LCA,这样一来答案就是distance[u]+distance[v]-2distance[LCA]. #include<iostream> #include<algorithm> #include<cstring> #include<cst…
2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU2586-LCA-tarjan离线版 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<casse…