POJ 1986 DIstance Query LCA水题】的更多相关文章

给出一棵树,对于每一个询问,给出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…
题目链接: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…
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…
题目链接: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…
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…
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…
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<stack> #include<queue> #include<iomanip> #include<cmath> #include<map> #include…
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…
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…