poj-1986 Distance Queries(lca+ST+dfs)
题目链接:
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 11531 | Accepted: 4068 | |
Case Time Limit: 1000MS |
Description
Input
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6
Sample Output
13
3
36 题意: 给一棵树;问两点之间的距离; 思路: lca的模板题,一开始莫名其妙的wa了,看了好久才发现是一个地方写反了;后来又tle,把cin改成scanf就好了;看来还是不能用cin; AC代码:
/*2014300227 1986 Accepted 13984K 485MS G++ 2077B*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=4e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,m,cnt,head[N],vis[N],a[*N],dep[N],first[N],dis[N],num,dp[*N][],p[N]; struct Edge
{
int to,next,val;
};
Edge edge[*N];
void add_edge(int s,int e,int va)
{
edge[cnt].to=e;
edge[cnt].next=head[s];
edge[cnt].val=va;
head[s]=cnt++;
}
int dfs(int x,int deep)
{
vis[x]=;
first[x]=num;
a[num++]=x;
dep[x]=deep;
for(int i=head[x];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(!vis[y])
{
dis[y]=dis[x]+edge[i].val;
dfs(y,deep+);
a[num++]=x;
}
}
}
int RMQ()
{
for(int i=;i<num;i++)
{
dp[i][]=a[i];
}
for(int j=;(<<j)<=num;j++)
{
for(int i=;i+(<<j)-<num;i++)
{
if(dep[dp[i][j-]]<dep[dp[i+(<<(j-))][j-]])dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
}
}
int query(int l,int r)
{
int temp=(int)(log((r-l+)*1.0)/log(2.0));
if(dep[dp[l][temp]]<dep[dp[r-(<<temp)+][temp]])return dp[l][temp];
else return dp[r-(<<temp)+][temp];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=;
num=;
for(int i=;i<=n;i++)
{
vis[i]=;
head[i]=-;
}
int u,v,w;
char s[];
for(int i=;i<m;i++)
{
scanf("%d%d%d%s",&u,&v,&w,s);
//cin>>u>>v>>w>>s;
add_edge(u,v,w);
add_edge(v,u,w);
}
dis[]=;
dfs(,);
RMQ();
int q,fx,fy;
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&fx,&fy);
int lca;
if(first[fx]<first[fy])
{
lca=query(first[fx],first[fy]);
}
else lca=query(first[fy],first[fx]);
printf("%d\n",dis[fx]+dis[fy]-*dis[lca]);
}
}
return ;
}
poj-1986 Distance Queries(lca+ST+dfs)的更多相关文章
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
随机推荐
- kettle转换之多线程
kettle转换之多线程 ETL项目中性能方面的考虑一般是最重要的.特别是所讨论的任务频繁运行,或一些列的任务必须在固定的时间内运行.本文重点介绍利用kettle转换的多线程特性.以优化其性能. ...
- C# 调用API接口处理公共类 自带JSON实体互转类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- 算法 Heap sort
// ------------------------------------------------------------------------------------------------- ...
- oracle select into相关
自定义参数输出: declare v_test integer :=0 ;beginselect count(*) into v_test from tf_estate_card t ;dbms_o ...
- 38: 立方根getCubeRoot
题目描述:计算一个数字的立方根,不使用库函数 •接口说明 原型:public static double getCubeRoot(double input) 输入:double 待求解参数 返回值:d ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Chrome自带恐龙小游戏的源码研究(五)
在上一篇<Chrome自带恐龙小游戏的源码研究(四)>中实现了障碍物的绘制及移动,从这一篇开始主要研究恐龙的绘制及一系列键盘动作的实现. 会眨眼睛的恐龙 在游戏开始前的待机界面,如果仔细观 ...
- 爬虫学习一系列:urllib2抓取网页内容
爬虫学习一系列:urllib2抓取网页内容 所谓网页抓取,就是把URL地址中指定的网络资源从网络中读取出来,保存到本地.我们平时在浏览器中通过网址浏览网页,只不过我们看到的是解析过的页面效果,而通过程 ...
- iOS中数组遍历的方法及比較
数组遍历是编码中非经常见的一种需求.我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点. 由于iOS是兼容C语言的.所以C语言里面的最最常见的for循环遍历是没有问题的. 本文中用的数组是获取的 ...
- spring boot 项目搭建时,各个依赖的作用
项目搭建页面 https://start.spring.io/ 各个依赖的作用 List of dependencies for Spring Boot 2.1.5.RELEASE Core DevT ...