poj 1986 Distance Queries(LCA)
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 "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input
* Lines ..+M: Same format as "Navigation Nightmare" * Line +M: A single integer, K. <= K <= , * Lines +M..+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines ..K: For each distance query, output on a single line an integer giving the appropriate distance.
E
E
S
N
W
S
Sample Output
Hint
Farms and are ++= apart.
Source
题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。结点数n最大为40000,k最大10000
分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。
这里用的是并查集的方法查找LCA,时间1000+ms,感觉有点慢
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m; int tot,qsize;
int head[N],qhead[N];
int vis[N];//标记
int dis[N];//距离
int fa[N]; struct Node
{
int from;
int to;
int next;
int cost;
}edge[N<<],qe[N];
void init()
{
tot=; qsize=; memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
memset(vis,,sizeof(vis));
memset(fa,,sizeof(fa));
memset(dis,,sizeof(dis));
memset(edge,,sizeof(edge));
memset(qe,,sizeof(qe));
}
void addEdge(int s,int u,int c)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].cost=c;
edge[tot].next=head[s];
head[s]=tot++;
}
void addQedge(int s,int u){
qe[qsize].from=s;
qe[qsize].to=u;
qe[qsize].next=qhead[s];
qhead[s]=qsize++;
} /////////////////////////////////////////////////////////////
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
/////////////////////////////////////////////////////////////
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
fa[u]=u;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
dis[v]=dis[u]+edge[i].cost;
tarjan(v);
fa[v]=u;
}
}
for(int i=qhead[u];i!=-;i=qe[i].next){
int v=qe[i].to;
if(vis[v]){
qe[i].cost=dis[u]+dis[v]-*dis[find(v)];
qe[i^].cost=qe[i].cost;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
int a,b,c;
char s[];
for(int i=;i<m;i++){
scanf("%d%d%d%s",&a,&b,&c,s);
addEdge(a,b,c);
addEdge(b,a,c);
}
int q;
scanf("%d",&q);
while(q--){
scanf("%d%d",&a,&b);
addQedge(a,b);
addQedge(b,a);
}
tarjan();
for(int i=;i<qsize;i+=){
printf("%d\n",qe[i].cost);
}
}
return ;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 200000
#define inf 1e12
vector<pair<int,int> >edge[N];
vector<pair<int,int> >que[N];
int n,m,q;
int ans[N];
int dis[N];
int fa[N];
int vis[N];
int sum;
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void LCA(int u,int p){
fa[u]=u;
for(int i=;i<edge[u].size();i++){
int v=edge[u][i].first;
if(v==p) continue;
dis[v]=dis[u]+edge[u][i].second;
LCA(v,u);
fa[v]=u;
}
vis[u]=;
if(sum==q) return;
for(int i=;i<que[u].size();i++){
int v=que[u][i].first;
if(vis[v]){
ans[que[u][i].second]=dis[u]+dis[v]-*dis[find(v)];
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){ for(int i=;i<N;i++){
edge[i].clear();
que[i].clear();
}
sum=;
memset(vis,,sizeof(vis)); char s[];
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d%s",&a,&b,&c,s);
edge[a].push_back(make_pair(b,c));
edge[b].push_back(make_pair(a,c));
} scanf("%d",&q);
for(int i=;i<q;i++){
int x,y;
scanf("%d%d",&x,&y);
que[x].push_back(make_pair(y,i));
que[y].push_back(make_pair(x,i));
ans[i]=;
}
dis[]=;
LCA(,); for(int i=;i<q;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
poj 1986 Distance Queries(LCA)的更多相关文章
- poj 1986 Distance Queries(LCA:倍增/离线)
计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)
题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...
- POJ1986 Distance Queries (LCA)(倍增)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12950 Accepted: 4577 ...
- POJ - 1986 Distance Queries(离线Tarjan算法)
1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - ...
- 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 ( 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 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
随机推荐
- Java迭代器深入理解及使用
Iterator(迭代器) 作为一种设计模式,迭代器可以用于遍历一个对象,对于这个对象的底层结构开发人员不必去了解. java中的Iterator一般称为“轻量级”对象,创建它的代价是比较小的.这里笔 ...
- linux指定动态运行库的位置
动态运行库在windows.linux下均广泛使用.windows下通常为dll文件,linux下为so文件.不过,对于部署程序,这两个系统查找依赖的运行库文件时却不一样.对于windows而言,优先 ...
- Javascript 解构的用处
对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量. let { log, sin, cos } = Math; 上面代码将Math对象的对数.正弦.余弦三个方法,赋值到对应的变量上,使用起 ...
- 常调用的Webservice接口 集合
1. 查询手机:http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=手机号码 2. 查询IP:http://www.yoda ...
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...
- android CMWAP, CMNET有何差别
什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...
- [HeadFist-HTMLCSS学习笔记][第四章Web镇之旅]
重要 访问一个目录,即是访问他的index <a>链接到网站,必须加http:// <a>的title属性,能预先知道链接信息 id属性 使得<a> 能再本地跳转. ...
- 移动前端开发之 viewport 的深入理解
移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或响 ...
- 【转载】ADO.NET与ROM的比较(1):ADO.NET实现CRUD
[转载]ADO.NET与ROM的比较(1):ADO.NET实现CRUD 转自周公 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hiberna ...
- 编译错误“The run destination My Mac 64-bit is not valid for Running the scheme '***',解决办法
1. iOS APP Project or Mac APP Project编译错误提示: “The run destination My Mac 64-bit is not valid for Ru ...