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…
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…
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=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…
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…
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…
题目链接: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 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…
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…
题目链接 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…
本题目输入格式同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的路径的最后一个…
计算树上的路径长度.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,…
1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - 2 * dis[lca(u, v)] 3. /* 离线算法,LCATarjan 复杂度O(n+Q); */ #include<iostream> #include<stdio.h> #include<string.h> using namespace std; ; ;//…
好像是模板题  当作练习题 不错:  要求任意两点之间的距离.可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先:  距离就变成了 dis[u]+dis[v] - 2*dis[ lca(u,v) ]  非常好的题目 #include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #include<cmath> using namespace…
http://poj.org/problem?id=1986 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14383   Accepted: 5063 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 lei…
[简介] 解决LCA问题的Tarjan算法利用并查集在一次DFS(深度优先遍历)中完成所有询问.换句话说,要所有询问都读入后才开始计算,所以是一种离线的算法. [原理] 先来看这样一个性质:当两个节点(u,v)的最近公共祖先是x时,那么我们可以确定的说,当进行后序遍历的时候,必然先访问完x的所有子树,其中包含u.v,然后才会返回到x所在的节点.这个性质就是我们使用Tarjan算法解决最近公共祖先问题的核心思想. 如上图所示,找出根节点到u得关键路径P ,已遍历的点位于路径P中某个点的子树中,当遍…
链接:https://vjudge.net/problem/HDU-2586 题意: 勇气小镇是一个有着n个房屋的小镇,为什么把它叫做勇气小镇呢,这个故事就要从勇气小镇成立的那天说起了,修建小镇的时候,为了让小镇有特色,镇长特地只修了n-1条路,并且规定说,所有在勇气小镇的村民,每一次出门必须规划好路线, 路线必须满足在到达终点之前绝对不走回头路.每个人都要这样,不然那个人就不配在小镇生活下去,因为他没有这个勇气.事实上,这并不能算一项挑战,因为n-1条路已经连通了每户人家,不回头地从起点到终点…
给出一棵树,对于每一个询问,给出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=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从树根出发进行后序深度优先遍历,设置vis数组实时记录是否已被访问. 每遍历完一棵子树r,把它并入以r的父节点p为代表元的集合.这时判断p是不是所要求的u, v节点之一,如果r==u,且v已访问过,则lca(u, v)必为v所属集合的代表元.p==v的情况类似. 我的第一道LCA问题的Tarjan算法…
Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accepted: 4340 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the…
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. 这题我真的要吐槽!!调试了好久啊!!不是MLE就是RE.... 查询数量没给,所以要用矩阵来存,这样还可以避免重复. 给边的时候不知道会不会有重复的点,所以讲道理应该用vector保存. getchar...我不知道为什么会RE... 其实ance数组可以不用的,只要每次处理子树的时候pa[v]…
<题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码的简洁,本代码用的是vector存图. #include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; ; vector<int>…
农夫约翰有N(2<=N<=40000)个农场,标号1到N.M(2<=M<=40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样,图中农场用F1..F7表示: 每个农场最多能在东西南北四个方向连结4个不同的农场.此外,农场只处在道路的两端.道路不会交叉而且每对农场间有且仅有一条路径.邻居鲍伯要约翰来导航,但约翰丢了农场的地图,他只得从电脑的备份中修复率.每一条道路的信息如下: 从农场23往南经距离10到达农场17 从农场1往东经…
题目链接: http://poj.org/problem?id=3728 题意: 给出一棵带点权值的树, 对于 q 组形如 x, y 的询问, 一个人要从 x 到 y(单向), 他可以在路上任意一点以此点的的权值买一件物品, 并在接下来的路程中任意一点将其以该点的权值卖出, 输出其最大收益, 若不能获益则输出 0 . 思路: 若不考虑时间复杂度的话对于询问 x, y. 可以先求出 lca(x, y), 然后再 x -> lca -> y 路径上求一下最大收益即可. 然而这样会 tle. 可以考…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 给出 n 个顶点 m 条边的一个森林, 有 k 个形如 x y 的询问, 输出 x, y 之间的最短路径. 思路: 如果将森林换成一棵树的话就是一道 lca 模板题了, 不过本题需要稍作改动. 解法1: tarjan 只需要先判断一下 x, y 是否在一颗树里面就 ok 了, 不过这道题的询问有点多, 很容易 mle. 代码: #include <iostream> #includ…
题目链接:LCA tarjan离线 这道题目WA无数发,最后还是参考了大神的blog 谁会想到因为一个输入外挂WA呢 大概是我的挂是假挂吧...orz(其实加上外挂,速度提升很多) 用链式前向星保存边的关系,同时为了节省空间也用前向星保存询问 注意要双向建边,同时dfs是先标记为访问状态 否则,会因为双向边的问题陷入死循环,或者改变了深搜的方向 #include <bits/stdc++.h> using namespace std; const int maxn = 500001; cons…
Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 18139   Accepted: 6248 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…
Distance Queries 时间限制: 1 Sec  内存限制: 128 MB 题目描述 约翰的奶牛们拒绝跑他的马拉松,因为她们悠闲的生活不能承受他选择的长长的赛道.因此他决心找一条更合理的赛道.此题的输入于第一题相同,紧接着下一行输入一个整数K,以后K行为K个"距离问题".每个距离问题包括两个整数,就是约翰感兴趣的两个农场的编号,请你尽快算出这两地之间的距离. N个点,N-1条边 输入 第1行:两个分开的整数:N和M:  第2..M+1行:每行包括4个分开的内容,F1,F2,L…