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 ...
随机推荐
- EasyUI这个框架用了好久了,总结一下遇到的问题和解决方法
1. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法: 我们之所以在页面中,只要书写相应easyui的class,Easyui就能成功渲染页面,这是因为解析器在 ...
- jquery实现图片的依次加载图片
css代码: ul#portfolio{margin:0;padding:0;} ul#portfolio li{float:left;margin:0 5px 0 0;width:250px;hei ...
- 深入浅出Attribute(二)
上篇里,我们把Attribute“粘”在类的成员方法上show了一把,让Attribute跟大家混了个脸儿熟.中篇里,我们将探讨“究竟什么是Attribute”和“如何创建及使用Attribute”这 ...
- oracle查询数据库资源位置
archival log list; 归档日志文件位置 select file_name from dba_data_files; 查询数据库文件位置 select parameter control ...
- Redis单台的安装部署及集群部署
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...
- FloatingActionButton 完全解析
鸿洋大神说的很明白这里就直接引用一下: FloatingActionButton 完全解析[Design Support Library(2)]
- 【Caffe】利用log文件绘制loss和accuracy(转载)
(原文地址:http://blog.csdn.net/liuweizj12/article/details/64920428) 在训练过程中画出accuracy 和loss曲线能够更直观的观察网络训练 ...
- C语言--循环结构
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- JSON格式之GSON解析
JSON格式之GSON解析 最近在做websocket相关,项目需要JSON解析.相较之下感觉google的GSON解析不错. JAVA后台 Gson提供了fromJson()方法来实现从Json相关 ...
- 使用jdk中keytool生成证书
-genkey 在用户主目录中创建一个默认文件".keystore",还会产生一个mykey的别名,mykey中包含用户的公钥.私钥和证书 -alias 产生别名 -keystor ...